# Before filter help

**URL:** <https://www.ruby-forum.com/t/before-filter-help/178755>\
**Category:** Rails\
**Created:** [November 10, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/before-filter-help/178755 "2009-11-10T21:23:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@Nicholas\_Y](https://www.ruby-forum.com/u/Nicholas_Y)\
**Post date:** [November 10, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/before-filter-help/178755/1 "2009-11-10T21:23:31Z")

</div>

First off, I want to apologize if this topic has been previously  
addressed on the list. I looked, but could have missed it.

I’m trying to run a subscription site, and have created a Subscription  
resource. I now have a table that holds a user\_id, sub\_user\_id, and  
expiration (in UTC.) I’ve constructed a query (that doesn’t blow up!)  
in my application controller that should check if a user’s  
subscription is valid:

def check\_subscription(current\_user\_id, content\_owner\_id)  
@subscription = Subscription.find(:all,  
:conditions =\> { :user\_id =\>  
current\_user\_id,  
:sub\_user\_id =\>  
content\_owner\_id,  
"((expiration

> ?))" =\> Time.now.utc })  
> if @subscription.nil?  
> redirect\_to root\_url  
> end  
> end

I would like to use this as a before filter in my other controllers,  
so I can keep the code DRY. However, I’m at a loss when it comes to  
passing in the current\_user\_id and content\_owner\_id into this method  
through a before\_filter. (For instance, every post can be marked as  
“subscription” required, and if the user has an active subscription,  
let them see the post.)

I hope all of this makes sense.

Thanks!

-Nicholas

---

<div class="post-metadata">

**Author:** ![Nicholas\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@Nicholas\_Y](https://www.ruby-forum.com/u/Nicholas_Y)\
**Post date:** [November 10, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/before-filter-help/178755/2 "2009-11-10T21:48:07Z")

</div>

Nicholas Y. wrote:

> I would like to use this as a before filter in my other controllers,  
> so I can keep the code DRY. However, I’m at a loss when it comes to  
> passing in the current\_user\_id and content\_owner\_id into this method  
> through a before\_filter. (For instance, every post can be marked as  
> “subscription” required, and if the user has an active subscription,  
> let them see the post.)

I think, in this case, I would add a name\_scope to User model that finds  
the user’s active/valid subscriptions:

current\_user.valid\_subscriptions(content\_owner)

Use a before\_filter to load the content\_owner in your controller(s).

I’ll leave it as an exercise for you to figure out how to write the  
named\_scope to do that.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@Nicholas\_Y](https://www.ruby-forum.com/u/Nicholas_Y)\
**Post date:** [November 10, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/before-filter-help/178755/3 "2009-11-10T22:09:10Z")

</div>

Try removing current\_user\_id and current\_owner\_id from the parameters  
list and moving them into ApplicationController as methods (like you  
would see with Authlogic/restful-authentication).

class ApplicationController  
before\_filter :check\_subscriptions

def current\_user\_id  
# however you collect this  
# or nil if it can’t be set  
end

def current\_owner\_id  
# however you collect this  
# or nil if it can’t be set  
end

def check\_subscriptions  
redirect\_to root\_url unless current\_user\_id && current\_owner\_id

```
# the rest of your method as defined above

```

end  
end

Also, you may want to look into using a third-party library for this.  
I use be9’s ACL9 authorization plugin to achieve something very  
similar in one of my apps.

Best,  
Jeff Tucker

On Nov 10, 1:25 pm, Nicholas Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Nicholas\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/779978/32.png) [@Nicholas\_Y](https://www.ruby-forum.com/u/Nicholas_Y)\
**Post date:** [November 11, 2009, 6:37pm UTC](https://www.ruby-forum.com/t/before-filter-help/178755/4 "2009-11-11T18:37:46Z")

</div>

On Nov 10, 1:25 pm, Nicholas Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> @subscription = Subscription.find(:all,
> :conditions => { :user_id =>
> 
> ```
> 
> current\_user\_id,  
> :sub\_user\_id =\>  
> content\_owner\_id,  
> “((expiration\> ?))” =\> Time.now.utc })

Somewhat offtopic, but how is the bit with a placeholder working? I  
just tried it against 2.3 and got an error about 'wrong number of bind  
variables".

–Matt J.
