# Model -\> with\_scope -\> session access?

**URL:** <https://www.ruby-forum.com/t/model-with-scope-session-access/144269>\
**Category:** Rails\
**Created:** [July 27, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269 "2008-07-27T23:43:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 27, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/1 "2008-07-27T23:43:03Z")

</div>

Hello,

I try to override the default find method for some of my models:

I want it to find only the users, which belong to a specific customer.  
Therefore I set an application-wide instance variable before rendering  
anything else:

class ApplicationController \< ActionController::Base  
before\_filter :cid, :uid

def cid  
@cid ||= session[:customer\_id] if logged\_in?  
end  
end

My model should do the following (but it doesnt work this way):

class User \< ActiveRecord::Base  
with\_scope :find, :conditions =\> {:customer\_id = cid}

…  
end

I know that accessing the session in models isn’t beatifull - so I  
tied via the application-method…  
Can somebody help me?

Thanks a lot!  
Best regards  
lp

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 27, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/2 "2008-07-27T23:50:06Z")

</div>

> Can somebody help me?

Got named scopes?

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 27, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/3 "2008-07-27T23:55:22Z")

</div>

Yeah, but this ins’t working either… When i try with:

class User \< ActiveRecord::Base  
with\_scope :owning, :conditions =\> {:customer\_id = cid}

…  
end

the following error occurs:

…  
…  
\*\* Starting Rails with development environment…  
Exiting  
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
base.rb:1667:in `method_missing_without_paginate': undefined local variable or method`cid’ for #Class:0x23ae54c (NameError)

Or do I missunderstand you? (sorry, my english is very bad)

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 28, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/4 "2008-07-28T23:17:30Z")

</div>

Doesnt’t have anyone an idea?  
Please help me, I think that other people are interessted in a  
solution too…

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 28, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/5 "2008-07-28T23:25:07Z")

</div>

Lp wrote:

> Doesnt’t have anyone an idea?

There may be no advanced solution. The simple solution might be to pass  
cid as  
an argument to a method on a model:

class User \< ActiveRecord::Base  
def find\_owners(cid)  
find(:all, :conditions =\> {:customer\_id =\> cid})  
end  
…  
end

From there, what more do you need?

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 28, 2008, 12:08am UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/6 "2008-07-28T00:08:48Z")

</div>

Sorry,

if you meant

class User \< ActiveRecord::Base  
NAMED\_scope :owning, :conditions =\> {:customer\_id = cid}

…  
end

this is producing the same error like above…

Heeeelp ☹  
Or are there any other solutions (no I don’t want to pass the id  
everytime… It’s not very DRY i think…)?

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 29, 2008, 12:09am UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/7 "2008-07-29T00:09:03Z")

</div>

Maybe I’m oversimplifying, but the general approach for scoping is

user  
has\_many :customers

Things are scoped already via your associations. No need to use  
with\_scope.

@user = current\_user  
@customers = @user.customers

@user = User.find(params[:user\_id[  
@customers = @user.customers.find(:all, :conditions =\> ["last\_contacted

> 

?", 10.days.ago], :order =\> “last\_name ASC”, :limit =\> 100

So… Would that work? A lot of this could be done dynamically too,  
through  
some basic metaprogramming.

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 29, 2008, 12:17am UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/8 "2008-07-29T00:17:14Z")

</div>

Hey…

I haven’t see the wood for the trees ^^

That’s a quite good solution - I get everytime the current user in the  
application-controller and use it in every action…

THANKS!!

---

<div class="post-metadata">

**Author:** ![LpLpLp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@LpLpLp](https://www.ruby-forum.com/u/LpLpLp)\
**Post date:** [July 28, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/model-with-scope-session-access/144269/9 "2008-07-28T23:44:28Z")

</div>

Yeah, that’s propably a solution (and the solution I’m currently  
using) - but that’s not very DRY…

I have about 6 models and 40 actions… and in every action I do  
something similar because of the rights/access-management:

```
Model.find(xyz, :condition => {:customer_id =>

```

session[:customer\_id]}

I don’t think it’s very DRY…?  
I can’t imagin that there isn’t a solution for it - that’s a common  
problem in my opinion?
