About a month ago I ran into a case where I needed to know if an AR 
object's association exists dynamically. I was building a CMS Engine.

I thought maybe this was a one off, but last week I found out my co-worker 
also had a need for this in the CMS he was building. So I've come to the 
community to see if this is something we think should be added into 
ActiveRecord.

I originally solved for this quite naively with the following solution

def is_association?(attribute)
  respond_to?(attribute) && !attribute_names.include?(attribute.to_s)
end 

and after trying to actually solve it in ActiveRecord I discovered 
reflections. I think this is easily solved with something like the 
following method inside lib/active_record/assocations.rb or 
lib/active_record/relation.rb

def has_association?(name)
  reflections.include? name
end

I'm new to contributing to open source and have been developing with Rails 
for about a year so any and all questions, comments, criticism is welcome.

Thanks!

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to