Saturday, June 7, 2008

before_save catch 22

I was attempting to update an attribute in my model using a value from a foreign key. I was not updating to what I was telling it to update to. So, I dove into the console to see what was happening. It turns out I was setting the code BACK to its original value in the before_save method. Not the behaviour I expected, because I didn't read the docs:

"Is called BEFORE"

# this hoses up the build_promotion process in the preferences screen,
# but it is needed for adding and updating kids... catch 22
person.promotion_sequence =

So, my value was getting overwritten by the old value. So, I commented out that line of code.

# person.promotion_sequence =

Now, I hit my next problem. I went to update my record using an existing form via the update_attributes method. This did not work because the attribute that needed to be updated was NOT in the params. So, here is how I fixed it :

@person.room_id = params[:person][:room_id]
@person.promotion_sequence =
@person.promotion_grade =

This now persists the promotion_grade. I'm not so certain this is the correct way to doing it.