Problem:
params = {:name => ''bob'', :sitter =>
''dog'', children => ''foo''}
ActiveRecord model ''FooBar'' with columns named
''name'', ''sitter'',
''houseboat''.
If I do
boo = FooBar.new(params)
I get a error stating that method ''children='' does not exist
for class
FooBar.
I think it would make more sense if the items in the hash with keys that
are not attributes of the model should be dropped without an error.
I have a solution for this but I wanted to make sure that this is
incorrect behavior for this method before I spend time fixing it.
Charles
--
Charles Dupont Computer System Analyst School of Medicine
Department of Biostatistics Vanderbilt University
Charles Dupont wrote:> Problem: > > params = {:name => ''bob'', :sitter => ''dog'', children => ''foo''}correction: params = {:name => ''bob'', :sitter => ''dog'', :children => ''foo''}> > ActiveRecord model ''FooBar'' with columns named ''name'', ''sitter'', > ''houseboat''. > > If I do > > boo = FooBar.new(params) > > I get a error stating that method ''children='' does not exist for class > FooBar. > > I think it would make more sense if the items in the hash with keys that > are not attributes of the model should be dropped without an error. > > I have a solution for this but I wanted to make sure that this is > incorrect behavior for this method before I spend time fixing it. > > Charles >-- Charles Dupont Computer System Analyst School of Medicine Department of Biostatistics Vanderbilt University
I hope raising exceptions on unknown attributes is/stays expected
behavior. I know it has saved me debugging time when I have had
typos in my field names.
It is easy to create sub-hashes from your views so you only include
the attributes you wish:
<input name="foo_bar[name]"/>
<input name="foo_bar[sitter]"/>
<input name="children"/>
Then your params will look like:
{:children => ''foo'', :foo_bar => {:name =>
''bob'', :sitter => ''dog''}}
So you create your new record like:
FooBar.new params[:foo_bar]
-Sean
On Nov 29, 2005, at 3:38 PM, Charles Dupont wrote:
> Charles Dupont wrote:
>> Problem:
>> params = {:name => ''bob'', :sitter =>
''dog'', children => ''foo''}
> correction:
> params = {:name => ''bob'', :sitter =>
''dog'', :children => ''foo''}
>
>> ActiveRecord model ''FooBar'' with columns named
''name'', ''sitter'',
>> ''houseboat''.
>> If I do
>> boo = FooBar.new(params)
>> I get a error stating that method ''children='' does
not exist for
>> class FooBar.
>> I think it would make more sense if the items in the hash with
>> keys that are not attributes of the model should be dropped
>> without an error.
>> I have a solution for this but I wanted to make sure that this is
>> incorrect behavior for this method before I spend time fixing it.
>> Charles
>
>
> --
> Charles Dupont Computer System Analyst School of Medicine
> Department of Biostatistics Vanderbilt University
> _______________________________________________
> Rails-core mailing list
> Rails-core@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-core
Charles Dupont wrote:> Charles Dupont wrote: > >> Problem: >> >> params = {:name => ''bob'', :sitter => ''dog'', children => ''foo''} > > correction: > params = {:name => ''bob'', :sitter => ''dog'', :children => ''foo''} > >> >> ActiveRecord model ''FooBar'' with columns named ''name'', ''sitter'', >> ''houseboat''. >> >> If I do >> >> boo = FooBar.new(params) >> >> I get a error stating that method ''children='' does not exist for >> class FooBar. >> >> I think it would make more sense if the items in the hash with keys >> that are not attributes of the model should be dropped without an error. >> >> I have a solution for this but I wanted to make sure that this is >> incorrect behavior for this method before I spend time fixing it. >> >> Charles >> > >I don''t think it would be a good idea to just silently ignore keys which aren''t valid column names of the underlying DB table. Chances are very high that the presence of an unknown key constitutes a programming error. Also, direct assignment (r.children = ...) is currently prohibited too. So this change would bring inconsistency as well. I suggest you define a new method for your purposes, if you absolutely insist on this behaviour. Like class ActiveRecord::Base def unsafe_assign_attributes(hash) attributes = hash.reject { |k,v| !self.class.columns_hash.has_key?(k) } end end -- stefan