associations book-author-serie
This commit is contained in:
parent
8553c371a5
commit
2bd4a9d742
8
app/models/author.rb
Normal file
8
app/models/author.rb
Normal file
@ -0,0 +1,8 @@
|
||||
class Author < ActiveRecord::Base
|
||||
establish_connection Rails.configuration.database_configuration["calibre"]
|
||||
|
||||
has_and_belongs_to_many :books,
|
||||
join_table: "books_authors_link",
|
||||
foreign_key: "author",
|
||||
association_foreign_key: "book"
|
||||
end
|
@ -1,3 +1,10 @@
|
||||
class Book < ActiveRecord::Base
|
||||
establish_connection Rails.configuration.database_configuration["calibre"]
|
||||
|
||||
has_and_belongs_to_many :authors,
|
||||
join_table: "books_authors_link",
|
||||
foreign_key: "book",
|
||||
association_foreign_key: "author"
|
||||
has_one :book_serie_link, foreign_key: "book"
|
||||
has_one :serie, through: :book_serie_link
|
||||
end
|
||||
|
8
app/models/book_serie_link.rb
Normal file
8
app/models/book_serie_link.rb
Normal file
@ -0,0 +1,8 @@
|
||||
class BookSerieLink < ActiveRecord::Base
|
||||
establish_connection Rails.configuration.database_configuration["calibre"]
|
||||
|
||||
self.table_name = "books_series_link"
|
||||
|
||||
belongs_to :book, foreign_key: "book"
|
||||
belongs_to :serie, foreign_key: "series"
|
||||
end
|
6
app/models/serie.rb
Normal file
6
app/models/serie.rb
Normal file
@ -0,0 +1,6 @@
|
||||
class Serie < ActiveRecord::Base
|
||||
establish_connection Rails.configuration.database_configuration["calibre"]
|
||||
|
||||
has_many :book_serie_link, foreign_key: "series"
|
||||
has_many :books, through: :book_serie_link
|
||||
end
|
@ -1 +1,7 @@
|
||||
<h1><%= @book.title %></h1>
|
||||
<ul>
|
||||
<% @book.authors.each do |author| %>
|
||||
<li><%= author.name %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<p>Serie: <%= @book.serie.name %>
|
||||
|
11
test/fixtures/authors.yml
vendored
Normal file
11
test/fixtures/authors.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
11
test/fixtures/book_serie_links.yml
vendored
Normal file
11
test/fixtures/book_serie_links.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
11
test/fixtures/series.yml
vendored
Normal file
11
test/fixtures/series.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
# This model initially had no columns defined. If you add columns to the
|
||||
# model remove the '{}' from the fixture names and add the columns immediately
|
||||
# below each fixture, per the syntax in the comments below
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
7
test/models/author_test.rb
Normal file
7
test/models/author_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AuthorTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
7
test/models/book_serie_link_test.rb
Normal file
7
test/models/book_serie_link_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class BookSerieLinkTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
7
test/models/serie_test.rb
Normal file
7
test/models/serie_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class SerieTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue
Block a user