鉴于我有以下模型:
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
而且,因为我使用动词+名词作为一对,我有以下助手(不可持久):
class Phrase < Struct.new(:verb, :noun); ...; end
我怎样才能把这个:
phrase = Phrase.new(my_verb, my_noun)
# sadface
Rule.create(verb: phrase.verb, noun: phrase.noun)
Rule.where(verb_id: phrase.verb.id).where(noun_id: phrase.noun.id)
# into this?
Rule.create(phrase: phrase)
Rule.where(phrase: phrase)
谢谢!