次のモデルがあるとします。
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)
ありがとう!