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.
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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---