# do a select from another table within a model

**URL:** <https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539>\
**Category:** rubyonrails-talk\
**Created:** [April 19, 2011, 8:59pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539 "2011-04-19T20:59:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 19, 2011, 8:59pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/1 "2011-04-19T20:59:57Z")

</div>

Hey all,

This right here:

&nbsp;&nbsp;scope :index\_blog, { &nbsp;&nbsp;&nbsp;&nbsp;:select =\> "blog\_posts.\*", &nbsp;&nbsp;&nbsp;&nbsp;:joins =\> "INNER JOIN categories ON category.id = blog\_posts.category\_id", &nbsp;&nbsp;&nbsp;&nbsp;:conditions =\>"blog\_posts.enabled = 1" &nbsp;&nbsp;}

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)

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. And I don't know how to select from blog posts from category model. I'm also curious if there is a better way to do this?

Thanks for response.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [April 19, 2011, 9:06pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/2 "2011-04-19T21:06:53Z")

</div>

Unless I misunderstand something can't you just say category.blog\_posts with :conditions specified for enabled = 1?

Colin

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 19, 2011, 9:11pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/3 "2011-04-19T21:11:50Z")

</div>

> 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" }
> 
> 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)
> 
> 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. And I don't know how to select from blog posts from category model. I'm also curious if there is a better way to do this?

Why are you trying to define a scope on blog posts in the categories model? Even if you fix the query, rails will still try and create instances of Category rather than instances of BlogPost

Fred

---

<div class="post-metadata">

**Author:** ![Kendall1](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@Kendall1](https://discuss.rubyonrails.org/u/Kendall1)\
**Post date:** [April 19, 2011, 9:12pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/4 "2011-04-19T21:12:28Z")

</div>

> Hey all, This right here:

> scope :index\_blog, { :select =\> “blog\_posts.\*”, :joins =\> “INNER JOIN categories ON [category.id](http://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](http://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.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 19, 2011, 9:55pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/5 "2011-04-19T21:55:52Z")

</div>

Actually, all i wanted to do initially was this:

def filter\_scoper(scoper) &nbsp;&nbsp;&nbsp;&nbsp;logger.info "The current class is #{self.class}" #CategoriesController

&nbsp;&nbsp;&nbsp;&nbsp;if params.has\_key? :filter &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send :"#{params[:filter]}\_filter\_scoper", scoper &nbsp;&nbsp;&nbsp;&nbsp;elsif self.class.respond\_to?(:blog\_filter\_scoper) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send :"blog\_filter\_scoper" &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scoper #call index\_scoper &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

Basically if the controller responds to blog\_filter\_scoper, then I want to pass blog\_filter\_scoper as the model:

&nbsp;&nbsp;def blog\_filter\_scoper &nbsp;&nbsp;&nbsp;&nbsp;BlogPost &nbsp;&nbsp;end

However, for some reason despite the fact that self.class outputs CategoriesController and when I load the categories page, it does not respond to the blog\_filter\_scoper method even though I defined blog\_filter\_scoper method within the categories controller.

I don't know why this is happening.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 19, 2011, 10:16pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/6 "2011-04-19T22:16:14Z")

</div>

ok I must never forget that in haml it works like this:

- @resources.each do |r| &nbsp;&nbsp;%h1= r.title &nbsp;&nbsp;%p= r.body

not this:

= @resources.each do |r| &nbsp;&nbsp;%h1- r.title &nbsp;&nbsp;%p- r.body

Otherwise, you will get an ActiveRecord array as an object.

---

<div class="post-metadata">

**Author:** ![Kendall1](https://avatars.discourse-cdn.com/v4/letter/k/a3d4f5/32.png) [@Kendall1](https://discuss.rubyonrails.org/u/Kendall1)\
**Post date:** [April 19, 2011, 10:38pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/7 "2011-04-19T22:38:54Z")

</div>

> Actually, all i wanted to do initially was this: def filter\_scoper(scoper) [logger.info](http://logger.info) “The current class is #{self.class}” #CategoriesController

> if params.has\_key? :filter send :“#{params[:filter]}\_filter\_scoper”, scoper elsif self.class.respond\_to?(:blog\_filter\_scoper)

Do you mean: elsif self.respond\_to?(:blog\_filter\_scoper) Note the lack of “.class” after self.

> send :“blog\_filter\_scoper” else scoper #call index\_scoper end end

> Basically if the controller responds to blog\_filter\_scoper, then I want to pass blog\_filter\_scoper as the model:

> def blog\_filter\_scoper BlogPost end

Yeah, looks like you’re wanting to call an instance method as I pointed out above.

> However, for some reason despite the fact that self.class outputs CategoriesController and when I load the categories page, it does not respond to the blog\_filter\_scoper method even though I defined blog\_filter\_scoper method within the categories controller.

> I don’t know why this is happening.

It would appear to me that your immediate problem is the line of code I pointed out above.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 19, 2011, 11:12pm UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/8 "2011-04-19T23:12:39Z")

</div>

> It would appear to me that your immediate problem is the line of code I pointed out above.

Yeah I got it now. Basically:

if self.class.respond\_to?

will only respond to class methods of the class, not instance methods and since attr\_accessor returns instance methods, that if will return false. So basically what I had to do was:

&nbsp;&nbsp;&nbsp;&nbsp;elsif self.class.method\_defined? :blog\_filter\_scoper

And that worked above. And then within my categories controller:

&nbsp;&nbsp;def blog\_filter\_scoper &nbsp;&nbsp;&nbsp;&nbsp;BlogPost.for\_posts 1 &nbsp;&nbsp;end

That will call the blogpost named scope and pass the argument 1 so now depending on which controller is invoked, we can pass different arguments to for\_posts to determine which posts to display.

The only thing I don't like now about this is that I am passing an integer 1. I'd rather do something like Category[:news].id - where rails will search the categories table with news value and then pull the id of that value which could be 1. I think that's better than hardcoding 1 right there. I guess that's the next thing I need to figure out.

Thanks for all responses.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 20, 2011, 7:26am UTC](https://discuss.rubyonrails.org/t/do-a-select-from-another-table-within-a-model/58539/9 "2011-04-20T07:26:06Z")

</div>

> > It would appear to me that your immediate problem is the line of code I pointed out above.
> 
> Yeah I got it now. Basically:
> 
> if self.class.respond\_to?
> 
> will only respond to class methods of the class, not instance methods and since attr\_accessor returns instance methods, that if will return false. So basically what I had to do was:

It's simpler than that. blah.respond\_to?(:foo) returns true if ruby believes you can do blah.foo, so in particular self.class.respond\_to?(:blah) checks (in your case) whether CategoriesController.blah can be called whereas self.respond\_to?(:blah) checks whether self.blah would work.

method\_defined? is a slightly more circuitous route to this and won't always work in a world like rails where methods are often created on the fly from method\_missing

Fred
