On Tuesday, April 19, 2011 2:59:57 PM UTC-6, Ruby-Forum.com User wrote:
>
> Hey all,
>
> This right here:
>
>   scope :index_blog, {
>     :select => "blog_posts.*",
>     :joins => "INNER JOIN categories ON category.id =
> blog_posts.category_id",
>     :conditions =>"blog_posts.enabled = 1"
>   }
>
First of all, "this right here" is in what model/file? I'm gonna guess from 
your error it is in your Category model in category.rb? 

> returns this:
>
> Mysql2::Error: Not unique table/alias: 'categories': SELECT blog_posts.*
> FROM `categories` INNER JOIN categories ON category.id =
> blog_posts.category_id WHERE (blog_posts.enabled = 1)
>
 
Another side problem: "ON category.id..." is your table named categories or 
category?
 

> But I am not trying to select blog_posts.* from categories. I am trying
> to select blog_posts.* from blog_posts. However, because I do this
> within the category model, it fills in categories.
>
Ahh, so we are in the Category model. This explains why the "FROM 
categories" part exists. Even though you're looking for blog_posts, don't 
worry about this.

Try:

  scope :index_blog, {
    :select => "blog_posts.*",
    :joins => "INNER JOIN blog_posts ON categories.id = 
blog_posts.category_id",
    :conditions =>"blog_posts.enabled = 1"
  }

> I'm also curious if there
> is a better way to do this?
>
That depends on what you're trying to do.

-- 
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 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to