我的模型被设置如下:行与通过至少一个匹配对象
class Record < ActiveRecord::Base
belongs_to :activity
has_many :codes, -> { order("codes.name") }, through: :activity
class Activity < ActiveRecord::Base
has_many :activity_code_links
has_many :codes, through: :activity_code_links
has_many :records
class ActivityCodeLink < ActiveRecord::Base
belongs_to :activity
belongs_to :code
class Code < ActiveRecord::Base
has_many :activity_code_links
has_many :activities, through: :activity_code_links
has_many :records, through: :activities
在控制器,我有记录一个ActiveRecord关系,@records。 我希望我的结果@records只包含具有特定代码的记录。
目前,我正在使用下面的解决方案;但它是没有效率的,并且也返回数组,而不是一个ActiveRecord关系:
@records = @records.reject { |record| (record.codes & [code]).empty? }
任何帮助,将不胜感激
干杯