Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created January 26, 2011 06:39
Show Gist options
  • Save ahawkins/796329 to your computer and use it in GitHub Desktop.
Save ahawkins/796329 to your computer and use it in GitHub Desktop.
# here is a small piece of trickery to assign the remindable assoication
# when reminder_attributes=(attrs) is called.
# When using nested_attributes/build we have a problem in the reminder model
# since Todo.new(:reminder_attributes => {:something => :good}).reminder returns a reminder
# however Todo.new(:reminder_attributes => {:valid => :attributes}).reminder.remindable returns nil
# this alias sets the todo as the in memory object so the reminder can access the parent
def reminder_attributes_with_assign_todo=(reminder_attrs)
self.reminder_attributes_without_assign_todo = reminder_attrs.merge(:remindable => self)
end
alias_method_chain :reminder_attributes=, :assign_todo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment