For has_(and_belongs_to_)many associations, I use nested hashes/arrays
in @params (ActionController#params). Thus, @params may look like this
{ :id => 1,
:employee => { :lastname => "new lastname"
:task_ids => [1, 2, 3] }
}
Of course, I''d like it best if new/create/update_attributes would
handle
this automatically behind the scenes, i.e., call employee.task_ids =
[1, 2, 3]. (A)
As this is not the case (yet), I have to handle this case manually in
controllers:
employee.task_ids = params[:employee][:task_ids]
On a sunny day it''ll even work. As params[:employee] can be nil,
there''s
a lurking failure. As a solution, I propose to implement params
explicitly like this (B)
def params(*args)
if args.empty?
@params
else
default_value = nil
if args.last.kind_of?(Hash)
opts = args.pop
default_value = opts[:default]
end
@params.get_at_path(args) || default_value
end
end
Not shown here is the implementation of get_at_path. For that I have
mixin (C) that works with hashes, traversing keys, as well as ordinary
objects, traversing methods. I find this *very* useful not only in this
particular case.
I''m looking forward to your comments. If there''s interest,
I''ll
write/submit patches for (A), (B), (C).
Michael
--
Michael Schuerig Those who call the shots
mailto:michael@schuerig.de Are never in the line of fire
http://www.schuerig.de/michael/ --Ani DiFranco, Not So Soft
Michael Koziarski
2005-Aug-08 13:46 UTC
[Rails-core] RFC: Safely accessing the params hash
> As this is not the case (yet), I have to handle this case manually in > controllers: > > employee.task_ids = params[:employee][:task_ids]> On a sunny day it''ll even work. As params[:employee] can be nil, there''s > a lurking failure. As a solution, I propose to implement params > explicitly like this (B)I don''t know that I like this, seems like a recipie for confusing, hard to track down bugs? It may become difficult to ''eyeball'' the code params[:blah] looks like params(:blah), but behaves differently. Isn''t two lines of code better here: ids = params[:employee][:task_ids] rescue nil do_such_and_such(ids) if ids I''m all for clever code, but perhaps making things explicit is better in this case. -- Cheers Koz
> employee.task_ids = params[:employee][:task_ids] > > On a sunny day it''ll even work. As params[:employee] can be nil, > there''s > a lurking failure.I''ve implemented a solution to this, perhaps similar to yours, that went like: hash.get_deep(:employee, :task_id) If a lookup fails at any point, nil is returned. And I''ve considered overriding Hash#[] to take multiple parameters, implementing the same functionality hash[:employee, :task_id] Not sure if having either built-in is a good idea, though.
Michael Schuerig
2005-Aug-08 17:13 UTC
[Rails-core] Re: RFC: Safely accessing the params hash
On Monday 08 August 2005 12:40, Michael Schuerig wrote:> For has_(and_belongs_to_)many associations, I use nested > hashes/arrays in @params (ActionController#params). Thus, @params may > look like this > > { :id => 1, > > :employee => { :lastname => "new lastname" > :task_ids => [1, 2, 3] } > > } > > Of course, I''d like it best if new/create/update_attributes would > handle this automatically behind the scenes, i.e., call > employee.task_ids = [1, 2, 3]. (A)Mea culpa. It already does... How did I miss this? Thanks, David. Michael -- Michael Schuerig Failures to use one''s frontal lobes mailto:michael@schuerig.de can result in the loss of them. http://www.schuerig.de/michael/ --William H. Calvin