option_groups_from_collection_for_select与范围

时间:2014-06-06 16:23:03

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

可以将option_groups_from_collection_for_select与集合的范围一起使用吗?

而不是获得所有时期:

 option_groups_from_collection_for_select(Modality.all, :periods, :name, :id, :name)

我想只获得活跃的那些:

 Period.active

谢谢!

1 个答案:

答案 0 :(得分:2)

NVM,

您在分组类中定义方法:

class Modality < ActiveRecord::Base
  has_many :periods

  def active_periods
    periods.active
  end
end

class Period < ActiveRecord::Base
  belongs_to :modality

  scope :active, ->{ where(active: true)}
end

option_groups_from_collection_for_select(Modality.all, :active_periods, :name, :id, :name)

希望它有所帮助。