associations book-author-serie

This commit is contained in:
Meutel 2015-06-28 16:09:16 +02:00
parent 8553c371a5
commit 2bd4a9d742
11 changed files with 89 additions and 0 deletions

8
app/models/author.rb Normal file
View 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

View File

@ -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

View 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
View 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

View File

@ -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
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_serie_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/series.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 AuthorTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

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

View File

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