On 24 Nov 2008, at 18:07, Sven Aas wrote:
> On Mon, Nov 24, 2008 at 12:49 PM, Frederick Cheung <[EMAIL PROTECTED] > > wrote: > It should do that by itself. You shouldn't need to do anything. Have > you tried it? > > Fred > > Yes, with code like this: > > named_scope :featured, :joins => :category_assignments, > :conditions => > ['category_assignments.featured = ?', true ] > > When I call > > Post.featured > > it works fine. But when I call, category.posts.featured I get > > ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous > column name: category_assignments.post_id > > because Rails generates this query: > > SELECT "posts".* FROM "posts" > INNER JOIN "category_assignments" ON category_assignments.post_id > = post.id > INNER JOIN category_assignments ON posts.id = > category_assignments.post_id > WHERE (("category_assignments".category_id = 1)) > AND ((category_assignments.featured = 't')) > > As you can see, category_assignments is being joined twice. The > named_scope needs to omit the category_assignments join when it gets > called on a Category instance. > Oops. I skimmed over the definition of your scope and didn't notice that there was a join. I'm going out on a limb here but from inside a procedural named scope then if there is a @proxy_owner instance variable or if it responds to proxy_owner/proxy_target then it;s an association. You could also check the current scope and see what joins are in there. Fred > I'm running this on Rails 2.1.1, by the way. > > -Sven > > PS: there was an error in my initial post. I specified > :conditions => {:category_assignments.featured => true } > where I ought to have written > :conditions => ['category_assignments.featured = ?', true] > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---