Reputation: 774
I am new to Rails and have a problem. I have three tables: books, authors, books_authors. In my view I would like to display two columns. First should display author's name and second all books that author has written decimal. In my books_authors table are foreign keys belongs to primary keys in books and authors tables. My schema:
ActiveRecord::Schema.define(version: 20150709110928) do
create_table "authors", force: :cascade do |t|
t.string "name"
t.string "surname"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "authors_books", id: false, force: :cascade do |t|
t.integer "author_id"
t.integer "book_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "authors_books", ["author_id"], name:
"index_author_books_on_author_id"
add_index "authors_books", ["book_id"], name:
"index_author_books_on_book_id"
create_table "books", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
and models looks like:
class Author < ActiveRecord::Base
has_many :authors_books
has_many :books, through: :authors_books
end
class Book < ActiveRecord::Base
has_many :authors_books
has_many :authors, through: :authors_books
end
How could I do it?
Upvotes: 0
Views: 75
Reputation: 344
Many-to-Many association in rails can be achieve through,
has_many:_______ ; through:______
Ex: Physicians has many patients through appointments.Also, physicians has many appointments.
Patients has many physicians through appointments. Also, here patients has many appointments.
Here the common entity is appointments. So DHH, has implemented many-to-many like this way.
physicians
has_many :appointments
has_many :patients, through: :appointments
patients
has_many :appointments
has_many :physicians, through: :appointments
appointments
belongs_to :physicians
belongs_to :patients
Hope it will helps.
Upvotes: 0
Reputation: 31
I think you should modify the code a little bit. There are two ways I knew to implement many-to-many association:
If you want use intermediate join table 'authors_books', you should use has_and_belongs_to_many, but in this case you cannot access authors_books table by model because there is not model about it.
If you want to store some data or info into the intermedidate join table, you should create the model by rails generator cmd like $ rails g model AuthorBook somedata:integer
, and use has_many through
. At last, delete the 'authors_books' table. The code is as follows:
class Author < ActiveRecord::Base
has_many :authorbooks
has_many :books, through: :authorbooks
end
class Book < ActiveRecord::Base
has_many :authorbooks
has_many :authors, through: :authorbooks
end
class Authorbook < ActiveRecord::Base
belongs_to :books
belongs_to :authors
end
Upvotes: 1
Reputation: 3268
In model authors_book, you should do this
class AuthorsBook < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
Each entry in authors_books table has author_id and books_id which holds this many to many assosiation. now when you do something this
@author.books
it will fetch all the books which that author has written.
You can easily traverse those books and display them.
Upvotes: 1