views:

75

answers:

2

I'm trying to make a helper which automatically picks the correct partial based on the types of objects returned by either ActiveRecord::Base#find or an association. Unfortuneatly I can't just look at the first element of the returned array because I want to pick the correct one in this case as well. If you call an association, it returns a proxy with the proxy_reflection method, which is exactly what I want, but it doesn't exist on the result of ActiveRecord::Base#find :(.

Example:

association_posts = Author.find(1).posts
association_posts.proxy_reflection.class_name # Returns "Post"
all_posts = Post.find(:all)
all_posts.proxy_reflection # no method exception, what do I call here instead?
A: 

I'm trying to do the same thing.

Author.find(1).posts returns an AssociationProxy, Post.find(:all) just returns an array (defined in ActiveRecord::Base).

A: 

I'm not sure if this is what you want, but try:

all_posts.first.class
klew