# Anonymous scopes against habtm relations

**URL:** <https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389>\
**Category:** Rails\
**Created:** [June 13, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389 "2008-06-13T14:40:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [June 13, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389/1 "2008-06-13T14:40:39Z")

</div>

I need help for using anonymous scopes in my application.  
Consider the problem of two model linked by a habtm relation. Let them  
be:

class FirstModel \< ActiveBase:Model  
has\_and\_belongs\_to\_many :second\_models  
end

class SecondModel \< ActiveBase:Model  
has\_and\_belongs\_to\_many :first\_models  
end

I set up the application with restful resources:

map.resources :first\_models, :has\_many =\> :second\_models  
map.resources :second\_models

So, calling the application at  
LOCAL\_ADDRESS/first\_models/1/second\_models  
the SecondModelController is called with: params[:first\_model\_id] = 1

Let say that I have to use pagination for displaying elements of the  
SecondModel.  
Es.

class SecondModelController \< ApplicationController  
def index  
@models = SecondModel.all.paginate(:page =\> params[:page])  
end  
end

Now, I want to add a filter to the index method in order to display the  
elements of SecondModel which are linked to the FirstModel instance  
passed as argument (through params[:first\_model\_id]). I tried to use  
anonymous scopes:

class SecondModelController \< ApplicationController  
def index  
@models = find\_models.paginate(:page =\> params[:page])  
end

private  
def find\_models  
scope = SecondModel.scoped({})

```
  if (params[:first_model_id])
    scope = scope.scoped :include => :first_models, :conditions =>

```

[‘first\_model = ?’, params[:first\_model\_id]]  
end

```
  scope
end

```

end

But it’s not working, since the query doesn’t find first\_model\_id as a  
column name in the ‘second\_models’ table.

Can someone please help me?  
Thank you, guys.

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [June 13, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389/2 "2008-06-13T14:42:40Z")

</div>

> class FirstModel \< ActiveBase:Model

Err… ActiveRecord:Base

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [June 13, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389/3 "2008-06-13T17:51:35Z")

</div>

Looks like it’s just formatting on your conditions. Shouldn’t it  
looks something like this:

:conditions=\>[‘first\_models.id = ?’, params[:first\_model\_id]]

On Jun 13, 8:42 am, Daniele Di Bernardo \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [June 13, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/anonymous-scopes-against-habtm-relations/140389/4 "2008-06-13T18:14:34Z")

</div>

AndyV wrote:

> Looks like it’s just formatting on your conditions. Shouldn’t it  
> looks something like this:
> 
> :conditions=\>[‘first\_models.id = ?’, params[:first\_model\_id]]
> 
> On Jun 13, 8:42 am, Daniele Di Bernardo \<rails-mailing-l…@andreas-

You’re right. Now everything works correctly. I remember I tried  
something like this, but probably I used ‘first\_model.id = ?’ instead of  
‘first\_models.id = ?’

Thank you!
