i new rails , have problem. have 3 tables: books, authors, books_authors. in view display 2 columns. first should display author's name , second books author has written decimal. in books_authors table foreign keys belongs primary keys in books , authors tables. schema:
activerecord::schema.define(version: 20150709110928) create_table "authors", force: :cascade |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 |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 |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 it?
in model authors_book, should this
class authorsbook < activerecord::base belongs_to :author belongs_to :book end each entry in authors_books table has author_id , books_id holds many many assosiation. when
@author.books it fetch books author has written.
you can traverse books , display them.
Comments
Post a Comment