関連付けのある2つのモデルがあります。あるモデルで before_save をセットアップして、他のモデルを変更したいと考えています。ここに私が持っているものがあります:
class Event < ActiveRecord::Base
belongs_to :leads
end
class Lead < ActiveRecord::Base
before_save :update_event
has_many :events, :dependent => :destroy
accepts_nested_attributes_for :events, :reject_if => lambda { |a| a[:title].blank? }
def update_event
self.events.title = "Testing to set the event title"
end
end
最終的にイベントには、システム内のさまざまなモデルの関連付けから設定されるタイトルなどの属性があります。上記の例では、Leads がその 1 つです。見込み客には可能なイベントのドロップダウン リストがあるので、ドロップダウンでの選択がイベント タイトルを設定するのに対し、before_save で何かをしたいと思います。そのような:
self.events.title = self.selected_event
私が得るエラーは次のとおりです。
undefined method `title=' for #<ActiveRecord::Relation:0x007fed3229d610>
私は明らかにこれを間違っています。何か助けていただければ幸いです。私はまだ解決策を探しており、それがわかったら自分で更新します。
ありがとうございました!