Anonymous scopes against habtm relations

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.

class FirstModel < ActiveBase:Model

Err… ActiveRecord:Base

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-

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!