Conditional named_scope chaining with params (Need help)

1 view
Skip to first unread message

David Sousa

unread,
Jan 5, 2010, 8:04:54 AM1/5/10
to rubyonra...@googlegroups.com
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/.

Chris Flipse

unread,
Jan 5, 2010, 9:07:56 AM1/5/10
to rubyonra...@googlegroups.com
On Tue, Jan 5, 2010 at 8:04 AM, David Sousa <li...@ruby-forum.com> 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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
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/

David Sousa

unread,
Jan 5, 2010, 9:57:47 AM1/5/10
to rubyonra...@googlegroups.com
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")
>

Reply all
Reply to author
Forward
0 new messages