# Conditional named\_scope chaining with params (Need help)

**URL:** <https://www.ruby-forum.com/t/conditional-named-scope-chaining-with-params-need-help/181095>\
**Category:** Rails\
**Created:** [January 5, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/conditional-named-scope-chaining-with-params-need-help/181095 "2010-01-05T14:04:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [January 5, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/conditional-named-scope-chaining-with-params-need-help/181095/1 "2010-01-05T14:04:54Z")

</div>

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 S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [January 5, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/conditional-named-scope-chaining-with-params-need-help/181095/2 "2010-01-05T15:10:17Z")

</div>

On Tue, Jan 5, 2010 at 8:04 AM, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> :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”)

> –

–  
// anything worth taking seriously is worth making fun of  
// [http://blog.devcaffeine.com/](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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [January 5, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/conditional-named-scope-chaining-with-params-need-help/181095/3 "2010-01-05T15:57:47Z")

</div>

Thanks Chirs, I will give it a try!

David S.

Chris F. 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”)
