【问题标题】:Ruby on Rails Index Action, Display RecordsRuby on Rails 索引操作,显示记录
【发布时间】:2014-09-16 15:25:06
【问题描述】:

抱歉这个无知的问题,我是 Ruby on Rails 的新手。

我有 3 个模型 - 雇主、雇员和公告

公告模型属于_雇主 雇主 has_many 公告 雇员属于_雇主

目前我可以使用

显示属于雇主的公告

@announcements = Announcement.where(:employer_id => current_employer.id)

我无法向员工显示属于雇主的公告。

我会使用什么查询来做到这一点

【问题讨论】:

  • 员工和公告有什么关系?也在雇员和雇主之间。
  • has_many through :employer.

标签: ruby-on-rails ruby-on-rails-3 activerecord


【解决方案1】:

如果你想经常使用EmployeeAnnouncement 关系,那么创建一个has_many through 的新关系,例如:

Class Employee
  has_one :employer
  has_many :announcements, through: :employer
end

然后你可以直接拥有属于员工的公告,只需执行以下操作:

@employee.announcements

深入了解可以参考2.4 The has_many :through Association

【讨论】:

    【解决方案2】:

    鉴于您提供的信息,我认为您希望显示特定雇员的公告,即雇主。例如:

    Employee -> Employer -> Announcement
    

    在这种情况下,您可以执行以下操作:

    @employeer = Employee.employeer #assuming this is a one-to-one relationship
    @announcements = Announcement.where(:employer_id => @employeer.id)
    

    【讨论】:

    • 感谢您的回复。我想向员工显示公告,并且公告属于雇主。 Employee 属于_to 和 Employer。而雇主有_many员工
    【解决方案3】:

    您应该能够使用

    显示所有雇主的公告
    current_employer.announcements
    

    假设雇主有_many雇员,给定一个@employee

    @employee.employer.announcements
    

    【讨论】:

      【解决方案4】:

      根据您的问题要求。您没有适当的关联。你应该有以下关联。

      雇主有_many公告

      Class Employer
        has_many :announcements
        has_many :employees, through :announcements
      end
      
      Class Employee
        belongs_to :announcement
      end
      
      Class Announcement
        belongs_to :employer, through :announcements
        belongs_to :announcement
      end
      

      现在像这样找到它:

      @announcements = current_employer.announcements
      

      还有这样的员工:

      @announcements.each do |announcement|
        @employees = announcement.employees
      end
      

      【讨论】:

        【解决方案5】:

        协会

        首先,您不应该对关联数据调用 .where 查询:

        @announcements = Announcement.where(:employer_id => current_employer.id)
        

        由于您是 RoR 的新手,我建议您快速阅读 ActiveRecord Association 文档,以便更深入地了解关联的工作原理。

        具体来说,您要确保每次拥有关联数据时,您都应该能够从“父”对象中调用它。 ActiveRecord 将获取您的关联并自动加载所需的数据(在foreign_keys 的帮助下):

        @announcements = current_employer.accouncements
        

        --

        修复

        为了让它工作,你需要这样做:

        #app/models/employer.rb
        class Employer < ActiveRecord::Base
           has_many :employees
           has_many :announcements
        end
        
        #app/models/employee.rb
        class Employee < ActiveRecord::Base
           belongs_to :employer
           delegate :announcements, to: :employer
        end
        
        #app/models/announcement.rb
        class Announcement < ActiveRecord::Base
           belongs_to :employer
        end
        

        这将使您能够调用以下内容:

        @employee = current_employer.employees.find 1
        @announcements = @employee.announcements
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-07-11
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-07
          相关资源
          最近更新 更多