Hello, I try to override the default find method for some of my models: I want it to find only the users, which belong to a specific customer. Therefore I set an application-wide instance variable before rendering anything else: class ApplicationController < ActionController::Base before_filter :cid, :uid def cid @cid ||= session[:customer_id] if logged_in? end end My model should do the following (but it doesnt work this way): class User < ActiveRecord::Base with_scope :find, :conditions => {:customer_id = cid} .... end I know that accessing the session in models isn''t beatifull - so I tied via the application-method... Can somebody help me? Thanks a lot! Best regards lp --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Can somebody help me?Got named scopes? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yeah, but this ins''t working either... When i try with: class User < ActiveRecord::Base with_scope :owning, :conditions => {:customer_id = cid} .... end the following error occurs: .. ... ** Starting Rails with development environment... Exiting /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:1667:in `method_missing_without_paginate'': undefined local variable or method `cid'' for #<Class:0x23ae54c> (NameError) Or do I missunderstand you? (sorry, my english is very bad) On 27 Jul., 23:49, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Can somebody help me? > > Got named scopes?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry, if you meant class User < ActiveRecord::Base NAMED_scope :owning, :conditions => {:customer_id = cid} .... end this is producing the same error like above... Heeeelp :( Or are there any other solutions (no I don''t want to pass the id everytime... It''s not very DRY i think...)? On 27 Jul., 23:54, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yeah, but this ins''t working either... When i try with: > > class User < ActiveRecord::Base > with_scope :owning, :conditions => {:customer_id = cid} > > .... > end > > the following error occurs: > > .. > ... > ** Starting Rails with development environment... > Exiting > /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ > base.rb:1667:in `method_missing_without_paginate'': undefined local > variable or method `cid'' for #<Class:0x23ae54c> (NameError) > > Or do I missunderstand you? (sorry, my english is very bad) > > On 27 Jul., 23:49, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Can somebody help me? > > > Got named scopes?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Doesnt''t have anyone an idea? Please help me, I think that other people are interessted in a solution too... On 28 Jul., 00:08, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry, > > if you meant > > class User < ActiveRecord::Base > NAMED_scope :owning, :conditions => {:customer_id = cid} > > .... > end > > this is producing the same error like above... > > Heeeelp :( > Or are there any other solutions (no I don''t want to pass the id > everytime... It''s not very DRY i think...)? > > On 27 Jul., 23:54, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Yeah, but this ins''t working either... When i try with: > > > class User < ActiveRecord::Base > > with_scope:owning, :conditions => {:customer_id = cid} > > > .... > > end > > > the following error occurs: > > > .. > > ... > > ** StartingRailswith development environment... > > Exiting > > /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ > > base.rb:1667:in `method_missing_without_paginate'': undefined local > > variable or method `cid'' for #<Class:0x23ae54c> (NameError) > > > Or do I missunderstand you? (sorry, my english is very bad) > > > On 27 Jul., 23:49, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Can somebody help me? > > > > Got named scopes?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yeah, that''s propably a solution (and the solution I''m currently using) - but that''s not very DRY... I have about 6 models and 40 actions... and in every action I do something similar because of the rights/access-management: Model.find(xyz, :condition => {:customer_id => session[:customer_id]} I don''t think it''s very DRY...? I can''t imagin that there isn''t a solution for it - that''s a common problem in my opinion? On 28 Jul., 23:24, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Lp wrote: > > Doesnt''t have anyone an idea? > > There may be no advanced solution. The simple solution might be to pass cid as > an argument to a method on a model: > > class User < ActiveRecord::Base > def find_owners(cid) > find(:all, :conditions => {:customer_id => cid}) > end > .... > end > > From there, what more do you need?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe I''m oversimplifying, but the general approach for scoping is user has_many :customers Things are scoped already via your associations. No need to use with_scope. @user = current_user @customers = @user.customers @user = User.find(params[:user_id[ @customers = @user.customers.find(:all, :conditions => ["last_contacted > ?", 10.days.ago], :order => "last_name ASC", :limit => 100 So... Would that work? A lot of this could be done dynamically too, through some basic metaprogramming. On Mon, Jul 28, 2008 at 4:44 PM, Lp <lampesberger.peter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yeah, that''s propably a solution (and the solution I''m currently > using) - but that''s not very DRY... > > I have about 6 models and 40 actions... and in every action I do > something similar because of the rights/access-management: > > Model.find(xyz, :condition => {:customer_id => > session[:customer_id]} > > I don''t think it''s very DRY...? > I can''t imagin that there isn''t a solution for it - that''s a common > problem in my opinion? > > On 28 Jul., 23:24, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Lp wrote: > > > Doesnt''t have anyone an idea? > > > > There may be no advanced solution. The simple solution might be to pass > cid as > > an argument to a method on a model: > > > > class User < ActiveRecord::Base > > def find_owners(cid) > > find(:all, :conditions => {:customer_id => cid}) > > end > > .... > > end > > > > From there, what more do you need? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey... I haven''t see the wood for the trees ^^ That''s a quite good solution - I get everytime the current user in the application-controller and use it in every action.. THANKS!! On 29 Jul., 00:08, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe I''m oversimplifying, but the general approach for scoping is > > user > has_many :customers > > Things are scoped already via your associations. No need to use with_scope. > > @user = current_user > @customers = @user.customers > > @user = User.find(params[:user_id[ > @customers = @user.customers.find(:all, :conditions => ["last_contacted > > ?", 10.days.ago], :order => "last_name ASC", :limit => 100 > > So... Would that work? A lot of this could be done dynamically too, through > some basic metaprogramming. > > On Mon, Jul 28, 2008 at 4:44 PM, Lp <lampesberger.pe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Yeah, that''s propably a solution (and the solution I''m currently > > using) - but that''s not very DRY... > > > I have about 6 models and 40 actions... and in every action I do > > something similar because of the rights/access-management: > > > Model.find(xyz, :condition => {:customer_id => > > session[:customer_id]} > > > I don''t think it''s very DRY...? > > I can''t imagin that there isn''t a solution for it - that''s a common > > problem in my opinion? > > > On 28 Jul., 23:24, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Lp wrote: > > > > Doesnt''t have anyone an idea? > > > > There may be no advanced solution. The simple solution might be to pass > > cid as > > > an argument to a method on a model: > > > > class User < ActiveRecord::Base > > > def find_owners(cid) > > > find(:all, :conditions => {:customer_id => cid}) > > > end > > > .... > > > end > > > > From there, what more do you need?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---