Ruby on rails 清理活动记录中属性组的api?

Ruby on rails 清理活动记录中属性组的api?,ruby-on-rails,activerecord,struct,Ruby On Rails,Activerecord,Struct,鉴于我有以下型号: class Rule < ActiveRecord::Base belongs_to :verb belongs_to :noun ... end class Verb < ActiveRecord::Base; end has_many :rules end class Noun< ActiveRecord::Base; end has_many :rules end 谢谢 T避免规则。其中(…)。其中(…)可以创建范围: cla

鉴于我有以下型号:

class Rule < ActiveRecord::Base
  belongs_to :verb
  belongs_to :noun
  ...
end

class Verb < ActiveRecord::Base; end
  has_many :rules
end

class Noun< ActiveRecord::Base; end
  has_many :rules
end
谢谢

T避免规则。其中(…)。其中(…)可以创建范围:

class Rule < ActiveRecord::Base
  scope :with_phrase, lambda { |p| where(verb: p.verb, noun: p.noun) }
end

我不知道为什么我没有马上想到这一点。我想可能是通过我的联系。不过这很容易

要清理
create
我只需要在
Rule

def phrase=(phrase)
  self.verb = phrase.verb
  self.noun = phrase.noun
end

# which allows me to
Rule.create(phrase: my_phrase)
为了清理arel
where
查询,我只需要在规则上创建一个作用域

def self.with_phrase(phrase)
  where(verb: p.verb, noun: p.noun)
end

# which allows me to
Rule.with_phrase(phrase)

对,一。我不知道为什么我在这个问题上感到困惑。添加了解决这两个问题的答案。
Rule.with_phrase( Phrase.new(my_verb, my_noun) )
def phrase=(phrase)
  self.verb = phrase.verb
  self.noun = phrase.noun
end

# which allows me to
Rule.create(phrase: my_phrase)
def self.with_phrase(phrase)
  where(verb: p.verb, noun: p.noun)
end

# which allows me to
Rule.with_phrase(phrase)