如何在显示页面中访问连接表上的额外属性?

时间:2017-07-04 15:19:36

标签: ruby-on-rails activerecord

我的模特看起来像这样:

class Project < ApplicationRecord
  has_many :comments
  has_many :contractor_projects
  has_many :contractors, through: :contractor_projects
  validates_presence_of :title, :contract_number, :category, :project_start_date, :project_end_date, :substantial_completion_date, :category, :solicitation_number, :project_officer, :location
  accepts_nested_attributes_for :contractor_projects
end

class Contractor < ApplicationRecord
  has_many :contractor_projects
  has_many :projects, through: :contractor_projects

  validates :name, presence: true

  validates :email, presence: true, uniqueness: true
 end

class ContractorProject < ApplicationRecord
  belongs_to :contractor
  belongs_to :project
end

ContractorProject模型有一个额外的属性#bid_status,我想反映在项目的节目页面上,但即使它在我提出它的时候也没有出现。< / p>

1 个答案:

答案 0 :(得分:0)

下面是您案例的示例方法

<% @contractors.each do |contractor| %>
  <%= contractor.bid_status %>
<% end %>

在show.html.erb中,你必须循环它,因为它可能会获得多个记录

randperm