我想调用同一个表中的不同字段,
分配者!=开放者
我无法调用分配了该主题的用户
<p>
<strong>Assignedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<p>
<strong>Openedby:</strong>
<%= @issue.user.try(:fullname) %>
</p>
<小时/>
class Issue < ApplicationRecord
belongs_to :project, foreign_key: :project_id, optional: true
belongs_to :user, foreign_key: :assignedby_id, optional: true
belongs_to :user, foreign_key: :openedby_id, optional: true
belongs_to :user, foreign_key: :closedby_id, optional: true
end
最佳答案
您不能为所有这些 belongs_to
关联指定相同的名称,它们需要不同,例如:
class Issue < ApplicationRecord
with_option optional: true do
belongs_to :project
belongs_to :assigner, class_name: 'User', foreign_key: :assignedby_id
belongs_to :creator, class_name: 'User', foreign_key: :openedby_id
belongs_to :closer, class_name: 'User', foreign_key: :closedby_id
end
end
关于mysql - Rails 调用同一个表中的不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55069593/