Rails Many_to_many 协会
Rails Many_to_many association
我是 Rails 的新手,遇到了问题。我有三个 table:书籍、作者、books_authors。在我看来,我想显示两列。首先应显示作者姓名,其次显示作者写有十进制的所有书籍。在我的 books_authors table 中,外键属于书籍和作者 table 中的主键。我的架构:
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
模型看起来像:
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
我该怎么做?
在模型 authors_book 中,您应该这样做
class AuthorsBook < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
authors_books table 中的每个条目都有 author_id 和 books_id,它们拥有这个多对多的关联。
现在当你做这件事时
@author.books
它将获取该作者所写的所有书籍。
你可以轻松地遍历那些书并展示它们。
我认为你应该稍微修改一下代码。我知道有两种实现多对多关联的方法:
- has_and_belongs_to_many
- has_many 至
如果你想使用中间连接 table 'authors_books',你应该使用 has_and_belongs_to_many,但在这种情况下你不能通过模型访问 authors_books table因为没有模型。
如果您想将一些数据或信息存储到中间连接 table,您应该通过 rails generator cmd 创建模型,如 $ rails g model AuthorBook somedata:integer
,并使用 has_many through
.最后,删除'authors_books' table。代码如下:
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
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
希望对您有所帮助。
我是 Rails 的新手,遇到了问题。我有三个 table:书籍、作者、books_authors。在我看来,我想显示两列。首先应显示作者姓名,其次显示作者写有十进制的所有书籍。在我的 books_authors table 中,外键属于书籍和作者 table 中的主键。我的架构:
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
模型看起来像:
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
我该怎么做?
在模型 authors_book 中,您应该这样做
class AuthorsBook < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
authors_books table 中的每个条目都有 author_id 和 books_id,它们拥有这个多对多的关联。 现在当你做这件事时
@author.books
它将获取该作者所写的所有书籍。
你可以轻松地遍历那些书并展示它们。
我认为你应该稍微修改一下代码。我知道有两种实现多对多关联的方法:
- has_and_belongs_to_many
- has_many 至
如果你想使用中间连接 table 'authors_books',你应该使用 has_and_belongs_to_many,但在这种情况下你不能通过模型访问 authors_books table因为没有模型。
如果您想将一些数据或信息存储到中间连接 table,您应该通过 rails generator cmd 创建模型,如 $ rails g model AuthorBook somedata:integer
,并使用 has_many through
.最后,删除'authors_books' table。代码如下:
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
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
希望对您有所帮助。