# Default value for scope in multi-account App, best way

**URL:** <https://www.ruby-forum.com/t/default-value-for-scope-in-multi-account-app-best-way/166548>\
**Category:** Rails\
**Created:** [April 27, 2009, 9:17am UTC](https://www.ruby-forum.com/t/default-value-for-scope-in-multi-account-app-best-way/166548 "2009-04-27T09:17:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [April 27, 2009, 9:17am UTC](https://www.ruby-forum.com/t/default-value-for-scope-in-multi-account-app-best-way/166548/1 "2009-04-27T09:17:14Z")

</div>

Hi,

I’m working on a multi-user, multi-account App where 1 account can have  
n users. It is very important that every user can only access info from  
its account. My approach is to add an account\_id to every model in the  
DB and than add a filter in every controller to only select objects with  
the current account\_id. I will use the authorization plugin.

Is this approach a good idea?

What is the best way to always set the account\_id for every object that  
is created without writing

object.account = @current\_account

in every CREATE action? Maybe a filter?

Also I’m not sure about the best way to implement the filter for the  
select options. I need something like a general condition: No matter  
what else appears in the SQL statement, there should always be a “WHERE  
account\_id = XY”.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [April 27, 2009, 9:59am UTC](https://www.ruby-forum.com/t/default-value-for-scope-in-multi-account-app-best-way/166548/2 "2009-04-27T09:59:44Z")

</div>

I know, I know, if you access Session-variables or Instance variables in  
your Model you didn’t understand the MVC pattern and “should go back to  
PHP”. But still, this could be very useful if you have - like us - a lot  
of controllers and actions where you don’t always want to write  
@current\_account.object.do\_something (not very DRY).

The solution I found is very easy:

Step 1: Add your current\_account to Thread.current, so for example

class ApplicationController \< ActionController::Base  
before\_filter :get\_current\_account

protected  
def get\_current\_account  
# somehow get the current account, depends on your approach  
Thread.current[:account] = @account  
end  
end

Step 2: Add a current\_account method to all your models

#/lib/ar\_current\_account.rb  
ActiveRecord::Base.class\_eval do  
def current\_account  
Thread.current[:account]  
end  
end

Step 3: VoilÃ¡, in your Models you can do something like this:

class MyModel \< ActiveRecord::Base

belongs\_to :account

# Set the default values

def initialize(params = nil)  
super  
self.account\_id ||= current\_account.id  
end

end

You could also work with something like the before\_validation callback  
in active\_record and then make with a validation sure the account is  
always set.

The same approach could be used if you always want to add the  
current\_user to every created object.

What do you think?

---

<div class="post-metadata">

**Author:** ![slafochmed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@slafochmed](https://www.ruby-forum.com/u/slafochmed)\
**Post date:** [April 27, 2009, 10:20am UTC](https://www.ruby-forum.com/t/default-value-for-scope-in-multi-account-app-best-way/166548/3 "2009-04-27T10:20:43Z")

</div>

Sorry, it has to be

#/lib/ar\_current\_account.rb  
ActiveRecord::Base.class\_eval do  
def self.current\_account  
Thread.current[:account]  
end  
end

of course!
