【问题标题】:Setting up associations / db references rails 4设置关联/数据库引用 rails 4
【发布时间】:2017-02-26 20:03:49
【问题描述】:

我有一张名为 bills 的表。比尔有_many 赞助商和has_many 共同赞助商。赞助商和共同赞助商都包含一份国会人员名单。我还有一张名为 congress_people 的桌子。在 congress_people 显示页面上,我想显示一张表格,显示国会人士赞助的账单和显示他们共同赞助的账单的表格。关于我如何做到这一点的任何想法?我知道要生成表格,但我不确定如何设置关联。我正在使用 rails 4.2 和 mysql2。谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 mysql2


    【解决方案1】:

    您可以使用has_many_through 关联,大致如下:

    class Bill < ActiveRecord::Base
      has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship"
      has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship"
    
      has_many :sponsors, class_name: 'CongressPerson', through: :sponsorships
      has_many :cosponsors, class_name: 'CongressPerson', through: :cosponsorships
    end
    
    class Sponsorship < ActiveRecord::Base
      # schema bill_id(INT), sponsor_id(INT), kind(STRING)
      belongs_to :bill
      belongs_to :sponsor, class_name: "CongressPerson"
    end
    
    class CongressPerson < ActiveRecord::Base
      has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship", foreign_key: :sponsor_id
      has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship", foreign_key: :sponsor_id
    
      has_many :sponsored_bills, through: :sponsorships, source: :bill
      has_many :cosponsored_bills, through: :cosponsorships, source: :bill
    
    end
    

    这是Sponsorship 表的基本迁移:

    class CreateSponsorships < ActiveRecord::Migration
      def change
        create_table :sponsorships do |t|
          t.references :bill
          t.references :sponsor
          t.string :kind
    
          t.timestamps
        end
      end
    end
    

    这将允许您进行如下调用:

    @bill.sponsors
    # => returns the congress_people sponsoring the bill
    
    @bill.cosponsors
    # => returns the congress_people cosponsoring the bill
    
    @congress_person.sponsored_bills
    # => returns the bills sponsored by the congress person
    
    @congress_person.cosponsored_bills
    # => returns the bills cosponsored by the congress person
    

    然后在您的CongressPerson 显示视图中:

    <%= @congress_person.sponsored_bills.each do |sponsored| %>
      # populate your HTML table row
    <% end %>
    <%= @congress_person.cosponsored_bills.each do |cosponsored| %>
      # populate your HTML table row
    <% end %>
    

    【讨论】:

    • 对于赞助表,我会为 bill_id 和赞助商 ID 使用 add_reference 吗?
    • @Taylor 我刚刚为Sponsorship 表添加了一个示例迁移。我还调整了活动记录关系以使其更清晰。希望对您有所帮助!
    • 所以我调整了一些东西,并且能够显示我的表单,但是我没有得到一个包含名称的下拉列表,而是得到一个带有 #<:activerecord_associations_collectionproxy:0x007fcd54ac3018> 的文本框.有什么想法吗?
    • @Taylor:听起来你有类似f.text_area :method 的东西,其中:method 是一个活跃的记录关联。您可能想要一个选择框,即f.select——尽管这也不适用于关联。我真的不确定你想做什么。我建议您为此提出一个单独的问题,有人可以帮助您!
    • @Taylor 如果这个答案对您有帮助,您会考虑接受它还是至少投票赞成?欣赏!
    猜你喜欢
    • 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
    相关资源
    最近更新 更多