How to get associated object created using after_create callback in rails transaction -
i have method saving session , event
def create_session new_session = newsession.new new_session.transaction new_session.name = 'somename' .... new_session.save! event = new_session.event #returning nil why? puts event.new_session end end
my newsession
class
class newsession < activerecord::base has_one :event, :class_name => sessionevent, :dependent => :delete after_create :create_event def create_event sessionevent.create(:name => 'eventname', :new_session => self) end end
now how can associated object created using after_create callback in rails transaction
try this:
class newsession < activerecord::base has_one :event after_create :create_event def create_event self.event.create(:name => 'eventname') end end
but make sure have 'new_session_id' in events table, if not add events table or define foreign_key in association as:
has_one :event, :foreign_key => "new_session"
and use own model code.
edit
rather save whole 'self' in event model save 'self.id' as:
def create_event event.create(:name => 'eventname', :new_session => self.id) end
and define assoication suggested above has_one :event, :foreign_key => "new_session"
hope you.
Comments
Post a Comment