Reputation: 858
I have two attributes, 'a_value' and 'b_id'. (Not their real names.) 'a_value' is stored on the file system, using some information from model 'B', referenced by 'b_id'.
So, my params object looks like:
params[:foo] = {"a_value"=>"nifty value","b_id"=>"38"}
for example.
Now, in foo_controller.rb:
foo = Foo.new(params[:foo])
But this fails with.
ActiveRecord::RecordNotFound: Couldn't find Foo without an ID
In Foo.a_value=(value) I have
...
self.my_path = self.b_id.the_path
...
It looks like Rails is doing the assignments in alphabetical order and panicking when b_id isn't there, even though it's present in the params hash and passes validation.
Can I force the order in which this assignment is done? Or can I create a before_filter that will do the b_id assignment before the remainder of the mass assignment happens?
Upvotes: 1
Views: 340
Reputation: 858
I figured it out!
The way to do this is as follows:
foo = Foo.new
foo[:b_id] = params[:foo][:b_id]
foo.update_attributes(params[:foo])
if foo.save...
You have to make sure your validations are in order, but it works.
It would be nicer if you could specify a partial ordering for your attributes without having to rename them.
Upvotes: 0