David Sousa
2010-Jan-05 13:04 UTC
Conditional named_scope chaining with params (Need help)
Hello there, I have a model that has more then one named_scope. In my action Index of the controller that handle this model I want to do this in a drier way: if params[:ownership] == "mine" @posts = Post.tagged_with(params[:tags], :on => :tags).owner(current_user.id).paginate :all, :page => params[:page], :order => ''created_at DESC'' else @posts = Post.tagged_with(params[:tags], :on => :tags).paginate :all, :page => params[:page], :order => ''created_at DESC'' end This is just a example, I have a lot of options that is why I don''t want to use the if else end structure. Thanks, David Sousa -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris Flipse
2010-Jan-05 14:07 UTC
Re: Conditional named_scope chaining with params (Need help)
On Tue, Jan 5, 2010 at 8:04 AM, David Sousa <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello there, > > I have a model that has more then one named_scope. > > In my action Index of the controller that handle this model I want to do > this in a drier way: > > if params[:ownership] == "mine" > @posts = Post.tagged_with(params[:tags], :on => > :tags).owner(current_user.id).paginate :all, :page => params[:page], > :order => ''created_at DESC'' > else > @posts = Post.tagged_with(params[:tags], :on => :tags).paginate > :all, :page => params[:page], :order => ''created_at DESC'' > end >chain = Posts.tagged_with(params[:tags], :on => :tags) chain = chain.owner(current_user.id) if params[:owner] chain = chain.posted_since(params[:since]) if params[:since] @posts = chain.paginate(:page => params[:page], :order => "created_at DESC")> > This is just a example, I have a lot of options that is why I don''t want > to use the if else end structure. > > Thanks, > > David Sousa > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- // anything worth taking seriously is worth making fun of // http://blog.devcaffeine.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Sousa
2010-Jan-05 14:57 UTC
Re: Conditional named_scope chaining with params (Need help)
Thanks Chirs, I will give it a try! David Sousa Chris Flipse wrote:> chain = Posts.tagged_with(params[:tags], :on => :tags) > chain = chain.owner(current_user.id) if params[:owner] > chain = chain.posted_since(params[:since]) if params[:since] > > @posts = chain.paginate(:page => params[:page], :order => "created_at > DESC") >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.