Possibly I just don''t see the easy way to set the target objects for
associations from the request parameters. There''s an
"impedance"
mismatch as, in the request parameters, objects are represented by
their id, whereas for manipulating the far end of an association real
objects are required.
It''s no big deal to resolve an id to the corresponding object and point
the association to it. For to-many associations a method like this one
helps
def set_association_collection(object, association_name, foreign_keys)
return unless foreign_keys
if !foreign_keys.kind_of?(Enumerable)
raise ArgumentError, ''foreign_keys must be an
Enumerable''
end
association_name = association_name.to_sym
association = object.send(association_name)
meta = object.class.reflect_on_association(association_name).options
associated_class_name = (
meta && meta[:class_name]) || association_name.capitalize
associated_class = eval(associated_class_name)
association.clear
foreign_keys.each do |id|
association << associated_class.find(id)
end
end
Still, the not-a-big-deals accumulate and obscure the essence of what
the action methods in a controller do. It would be much cleaner, if
associations had support for working with foreign keys built-in.
Michael
--
Michael Schuerig There is no matrix,
mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org only reality.
http://www.schuerig.de/michael/ --Lawrence Fishburn