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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> class FirstModel < ActiveBase:ModelErr... ActiveRecord:Base -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> > class FirstModel < ActiveBase:Model > > Err... ActiveRecord:Base > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---