【问题标题】:Referencing multiple models in Rails在 Rails 中引用多个模型
【发布时间】:2016-01-23 07:00:36
【问题描述】:

我的部门有很多职位,有很多船员。我正在尝试制作船员总名单的页面,按他们的职位分组,这些职位按他们的部门分组。我知道下面的代码是错误的,但希望有人能指出我正确的方向。

<% @departments.each do |dept| %>
    <% if Department.position.include? crewmember %>
      <%= dept.department %><br />
      <% @positions.each do |pos| %>
        <% if Position.crewmember.any? %>
          <%= pos.position %><br />
          <%= pos.position.crewmember %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>

编辑- 我的模型:

class Crewmember < ActiveRecord::Base
    belongs_to :production
    belongs_to :callsheet

    validates :firstname, :email, presence: true

    scope :visible, where(visible: true)

    def name
        "#{firstname} #{lastname}"
    end

end

class Department < ActiveRecord::Base
    has_many :positions
    belongs_to :production

    attr_accessible :department
    validates :department, presence: true
end

class Position < ActiveRecord::Base
    belongs_to :department

    attr_accessible :department_id, :position, :department

end

【问题讨论】:

  • 了解您的表格是什么样子可能会有所帮助
  • 您可以包含您的模型代码,以使您的问题清晰易懂。
  • 我编辑了问题以包含模型信息

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 model


【解决方案1】:

你的问题不清楚。但是,据我了解,我认为这就是您所需要的:

<% @departments.each do |dept| %>
      <%= dept.name %><br />
      <% dept.positions.each do |pos| %>
          <%= pos.name %><br />
          <% pos.crewmembers.each do |cm| %>
             <%= cm.name %><br />
          <% end %>
      <% end %>
<% end %>

【讨论】:

  • 机组人员与职位/部门没有关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2012-02-07
  • 2014-11-12
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多