【问题标题】:Rails Many_to_many associationRails 多对多关联
【发布时间】:2015-07-13 11:17:30
【问题描述】:

我是 Rails 新手,遇到了问题。我有三个表:书籍、作者、书籍作者。在我看来,我想显示两列。首先应该显示作者的名字,其次是作者写过十进制的所有书籍。在我的 books_authors 表中,外键属于 book 和 authors 表中的主键。我的架构:

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

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails foreign-keys many-to-many relational-database


    【解决方案1】:

    在模型 authors_book 中,您应该这样做

    class AuthorsBook < ActiveRecord::Base
       belongs_to :author
       belongs_to :book
    end
    

    authors_books 表中的每个条目都有 author_id 和 books_id,它们保存了这个多对多的关联。 现在当你这样做时

      @author.books
    

    它将获取该作者撰写的所有书籍。

    您可以轻松地遍历这些书籍并显示它们。

    【讨论】:

    【解决方案2】:

    我认为你应该稍微修改一下代码。我知道实现多对多关联的方法有两种:

    1. has_and_belongs_to_many
    2. has_many 通过

    如果你想使用中间连接表'authors_books',你应该使用has_and_belongs_to_many,但是在这种情况下你不能按模型访问authors_books表,因为它没有模型。

    如果你想将一些数据或信息存储到中间连接表中,你应该通过rails generator cmd创建模型,如$ rails g model AuthorBook somedata:integer,并使用has_many through。最后,删除“authors_books”表。代码如下:

    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
    

    【讨论】:

      【解决方案3】:
      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       
      

      希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多