On Sunday 19 June 2005 02:22, David Corbin wrote:> Is there any support for finding out about AR associations
> "reflectively"? That is, dynamicaly by making some sort of call
that
> that tells you what associations a given class might have?
See reflect_on_all_associations and reflect_on_association
A drawback of those methods is that the association options they return,
among other things, only contain what''s "unusual" about the
association. IMHO that''s barely usable by itself and I''ve
wrapped a
utility method around this
def options_for_association(klass, association_name)
if meta = klass.reflect_on_association(association_name.to_sym)
options = meta.options
options[:class_name] ||= Inflector.camelize(
association_name.singularize)
options[:foreign_key] ||= Inflector.foreign_key(
association_name.singularize)
options[:association_foreign_key] ||= Inflector.underscore(
Inflector.demodulize(klass.name)) + ''_id''
if block_given?
yield options
else
options
end
end
end
I''m not completely sure if this completely correct in all cases,
though...
Michael
--
Michael Schuerig Face reality and stare it down
mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull,
Silver River Turning
http://www.schuerig.de/michael/