It is almost, but not quite, completely unlike tea.

Thursday, February 22, 2007

create vs. create! vs. new in Rails

I've been spending a little time lately on updating The Secret App to remove cruft (Login / User Engine being the two main sources of said cruft).  Now that I'm no longer locked into an old revision of edge, I have noticed that Rails's ActiveRecord behavior seems to have changed a bit.  Before, I could get away with saying
@widget = current_user.widgets.create!(params[:widget])
Now, that gets me a big fat nil in @widget.user_id.  Unfortunately,
@widget = current_user.widgets.new(params[:widget])
gives me the same darned thing.  The only way I could get the user_id to  fill properly was with
@widget = current_user.widgets.create(params[:widget])
Maybe it's something I am doing wrong, but I haven't found it yet.  I wouldn't put much effort into it, but I really like the clean syntax I get from #create! and so it has gotten under my skin.  Any ideas?  Anyone?  Bueller?