【问题标题】:Rails 3 + ActiveAdmin - filtering a has_many association from the parent model indexRails 3 + ActiveAdmin - 从父模型索引中过滤 has_many 关联
【发布时间】:2015-02-02 15:22:32
【问题描述】:

我有两个模型:

class Worker
  has_many :location_preferences
end

class LocationPreference
  attr_accessible :location
  belongs_to :worker
end

在 ActiveAdmin Worker 索引中,我希望能够通过具有选定 :location 的位置首选项(或者,理想情况下,通过多个位置首选项查找工作人员的复选框)来过滤工作人员。

在其他情况下,我可以像这样过滤子索引:

ActiveAdmin.register Account

filter :user_last_name

...使用提供的last_name 查找用户拥有的帐户。但是我没有太多运气通过子类的属性过滤父类。我可以制作一个范围按钮,但我已经有一堆用于此 AA 资源的范围按钮,并且不想挤满它。另外,似乎应该有某种方法可以做到这一点,而不必为每个可以想象的过滤器选项编写范围。

提前感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord scope activeadmin


    【解决方案1】:

    添加has_many :locations, through: :location_preferences 将允许将位置过滤器添加到Worker

    class Worker
      has_many :location_preferences
    
      # Add has many through relation
      has_many :locations, through: :location_preferences
    end
    

    然后可以使用位置过滤器定义 ActiveAdmin 资源。

    ActiveAdmin.register Worker do
      filter :locations, as: :check_boxes
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多