associations book-tag-publisher-language

This commit is contained in:
Meutel 2015-06-28 16:39:18 +02:00
parent 2bd4a9d742
commit 5ee2ea8e48
18 changed files with 162 additions and 1 deletions

View File

@ -5,6 +5,18 @@ class Book < ActiveRecord::Base
join_table: "books_authors_link", join_table: "books_authors_link",
foreign_key: "book", foreign_key: "book",
association_foreign_key: "author" association_foreign_key: "author"
has_and_belongs_to_many :tags,
join_table: "books_tags_link",
foreign_key: "book",
association_foreign_key: "tag"
has_and_belongs_to_many :languages,
join_table: "books_languages_link",
foreign_key: "book",
association_foreign_key: "lang_code"
has_one :book_publisher_link, foreign_key: "book"
has_one :publisher, through: :book_publisher_link
has_one :book_serie_link, foreign_key: "book" has_one :book_serie_link, foreign_key: "book"
has_one :serie, through: :book_serie_link has_one :serie, through: :book_serie_link
end end

View File

@ -0,0 +1,8 @@
class BookPublisherLink < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["calibre"]
self.table_name = "books_publishers_link"
belongs_to :book, foreign_key: "book"
belongs_to :publisher, foreign_key: "publisher"
end

8
app/models/language.rb Normal file
View File

@ -0,0 +1,8 @@
class Language < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["calibre"]
has_and_belongs_to_many :books,
join_table: "books_languages_link",
foreign_key: "lang_code",
association_foreign_key: "book"
end

6
app/models/publisher.rb Normal file
View File

@ -0,0 +1,6 @@
class Publisher < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["calibre"]
has_many :book_publisher_link, foreign_key: "publishers"
has_many :books, through: :book_publisher_link
end

8
app/models/tag.rb Normal file
View File

@ -0,0 +1,8 @@
class Tag < ActiveRecord::Base
establish_connection Rails.configuration.database_configuration["calibre"]
has_and_belongs_to_many :books,
join_table: "books_tags_link",
foreign_key: "tag",
association_foreign_key: "book"
end

View File

@ -4,4 +4,15 @@
<li><%= author.name %></li> <li><%= author.name %></li>
<% end %> <% end %>
</ul> </ul>
<p>Serie: <%= @book.serie.name %> <ul>
<% @book.languages.each do |language| %>
<li><%= language.lang_code %></li>
<% end %>
</ul>
<ul>
<% @book.tags.each do |tag| %>
<li><%= tag.name %></li>
<% end %>
</ul>
<p>Serie: <%= @book.serie.name %></p>
<p>Publisher: <%= @book.publisher.name %></p>

11
test/fixtures/book_language_links.yml vendored Normal file
View 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_publisher_links.yml vendored Normal file
View 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_tag_links.yml vendored Normal file
View 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/languages.yml vendored Normal file
View 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/publishers.yml vendored Normal file
View 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/tags.yml vendored Normal file
View 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

View File

@ -0,0 +1,7 @@
require 'test_helper'
class BookLanguageLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class BookPublisherLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class BookTagLinkTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class LanguageTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class PublisherTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/models/tag_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class TagTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end