Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to store data to be used in the callback in ActiveRecord model
    primarykey
    data
    text
    <p>user can assign multiple predefined <code>events</code> to a <code>room</code> however, sometimes they might create new <code>events</code> on the fly. </p> <p>I want to capture these new events and add them to a separate table <code>userevent</code> </p> <p>Below is my approach. I am trying to save all the events that don't exist into a <code>:user_events</code> and then callback <code>add_new_events_to_userevents</code> on after_save.</p> <pre><code> after_save :add_new_events_to_userevents attr_reader :event_tokens, :user_events attr_accessible :user_events #not real column in table attr_accessible :fulladdress, :title, :description, :cost, :email, :maximum_capacity, :phone, :user_id, :property_id def add_new_events_to_userevents print "total new events:" + self.user_events.size self.user_events.each do |i| u = Userevent.new u.name = i u.approved = false u.room_id = self.id u.user_id = self.user_id u.save end end def event_tokens=(ids) events = ids.split(',') allowed_events = [] userevents = [] events.each do |i| i = i.strip events = Event.select(:id).where("upper(name) = ?", i.upcase); event = (events.blank? ? nil : events.first) if event.present? allowed_events &lt;&lt; event[:id] else userevents &lt;&lt; i end end self.event_ids = allowed_events #these are events that exist self.user_events = userevents #these are new events, need them in after_save end </code></pre> <p>I am getting an error </p> <pre><code>You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.size </code></pre> <ul> <li>how can I store all new user events and then use them in my <code>after_save</code> callback?</li> <li>in the <code>after_save</code> callback if I call <code>self.id</code> and <code>self.user_id</code> will that give me <code>id</code> and <code>user_id</code> of the record that was just saved to the DB?</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload