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
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core