From 2bd4a9d742a88ba5e77a008006f63443fdf9b3dc Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 28 Jun 2015 16:09:16 +0200 Subject: [PATCH] associations book-author-serie --- app/models/author.rb | 8 ++++++++ app/models/book.rb | 7 +++++++ app/models/book_serie_link.rb | 8 ++++++++ app/models/serie.rb | 6 ++++++ app/views/books/show.html.erb | 6 ++++++ test/fixtures/authors.yml | 11 +++++++++++ test/fixtures/book_serie_links.yml | 11 +++++++++++ test/fixtures/series.yml | 11 +++++++++++ test/models/author_test.rb | 7 +++++++ test/models/book_serie_link_test.rb | 7 +++++++ test/models/serie_test.rb | 7 +++++++ 11 files changed, 89 insertions(+) create mode 100644 app/models/author.rb create mode 100644 app/models/book_serie_link.rb create mode 100644 app/models/serie.rb create mode 100644 test/fixtures/authors.yml create mode 100644 test/fixtures/book_serie_links.yml create mode 100644 test/fixtures/series.yml create mode 100644 test/models/author_test.rb create mode 100644 test/models/book_serie_link_test.rb create mode 100644 test/models/serie_test.rb diff --git a/app/models/author.rb b/app/models/author.rb new file mode 100644 index 0000000..4707ab1 --- /dev/null +++ b/app/models/author.rb @@ -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 diff --git a/app/models/book.rb b/app/models/book.rb index 0044c2f..6d70287 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -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 diff --git a/app/models/book_serie_link.rb b/app/models/book_serie_link.rb new file mode 100644 index 0000000..3b41d06 --- /dev/null +++ b/app/models/book_serie_link.rb @@ -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 diff --git a/app/models/serie.rb b/app/models/serie.rb new file mode 100644 index 0000000..d7bbc19 --- /dev/null +++ b/app/models/serie.rb @@ -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 diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 4afce77..8490ce2 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -1 +1,7 @@

<%= @book.title %>

+ +

Serie: <%= @book.serie.name %> diff --git a/test/fixtures/authors.yml b/test/fixtures/authors.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/authors.yml @@ -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 diff --git a/test/fixtures/book_serie_links.yml b/test/fixtures/book_serie_links.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/book_serie_links.yml @@ -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 diff --git a/test/fixtures/series.yml b/test/fixtures/series.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/series.yml @@ -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 diff --git a/test/models/author_test.rb b/test/models/author_test.rb new file mode 100644 index 0000000..92b5e1f --- /dev/null +++ b/test/models/author_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AuthorTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/book_serie_link_test.rb b/test/models/book_serie_link_test.rb new file mode 100644 index 0000000..eaf1143 --- /dev/null +++ b/test/models/book_serie_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class BookSerieLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/serie_test.rb b/test/models/serie_test.rb new file mode 100644 index 0000000..a6ce850 --- /dev/null +++ b/test/models/serie_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SerieTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end