dynamically set named_scope based on current_user

J

John Merlino

Hey all,
I keep getting the following error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.size

Based on the current user, when they navigate to a page, I want to limit
them to what they can see based on their site. Problem is there is no
association between site and user table directly. A contact has_one user
(user information is stored in current_user variable). A site has_many
contacts. And a site has_many students, where students table has a
foreign key of site_id. So there is a link between students and site, so
when the current user navigates to students page, they can only see
students from same site as them. I can do this by hard coding a number
in a named_scope to only display students for the site of the
current_user. But different users will belong to different sites so when
logged in, the site their associated with will change. That's the
problem - to dynamically set that value in a named_scope. This is what I
have:

StudentsController

def index_scoper
if current_user.role_id == 8
super.site_staff_limit while current_user[:site_id] #The problem is
the user table has no site_id. There is no direct link between the users
table and sites table. However, there is a link between users and
contacts and then site and contacts and then site and students, where
students table has site_id.
else
super.with_state.with_site
end
end

Student Model
named_scope :site_staff_limit, lambda {|site_id| {:conditions =>
{:site_id => site_id}}}

Thanks for any suggestions.
 
J

John Merlino

John said:
Hey all,
I keep getting the following error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.size

Based on the current user, when they navigate to a page, I want to limit
them to what they can see based on their site. Problem is there is no
association between site and user table directly. A contact has_one user
(user information is stored in current_user variable). A site has_many
contacts. And a site has_many students, where students table has a
foreign key of site_id. So there is a link between students and site, so
when the current user navigates to students page, they can only see
students from same site as them. I can do this by hard coding a number
in a named_scope to only display students for the site of the
current_user. But different users will belong to different sites so when
logged in, the site their associated with will change. That's the
problem - to dynamically set that value in a named_scope. This is what I
have:

StudentsController

def index_scoper
if current_user.role_id == 8
super.site_staff_limit while current_user[:site_id] #The problem is
the user table has no site_id. There is no direct link between the users
table and sites table. However, there is a link between users and
contacts and then site and contacts and then site and students, where
students table has site_id.
else
super.with_state.with_site
end
end

Student Model
named_scope :site_staff_limit, lambda {|site_id| {:conditions =>
{:site_id => site_id}}}

Thanks for any suggestions.

relationship between tables:

users: contact_id
contact: primary key, contactable_id, contactable_type
site: primary key
student: site_id

User model
belongs_to :contact

Contact model
has_one :user
belongs_to :contactable, :polymorphic => true, :dependent => :destroy

Site model
has_many :contacts, :as => :contactable
has_many :students

Students model
belongs_to :site

This successfully limits the students by site:
StudentsController
def index_scoper
if current_user.role_id == 8
super.site_staff_limit
else
super.with_state.with_site
end
end

Students model
named_scope :site_staff_limit, :conditions => {:site_id => 1}

The problem is different users will belong to different sites, so they
can only access student records of the site they belong to. I am having
difficulty making that named_scope above dynamic enough to accomplish
this.
 
P

Phillip Gawlowski

The problem is different users will belong to different sites, so they
can only access student records of the site they belong to. I am having
difficulty making that named_scope above dynamic enough to accomplish
this.

Since I suspect this is a Rails app, you will have better success when
asking the Rails community:
http://rubyonrails.org/community
 
B

Brian Candler

John said:
That's the
problem - to dynamically set that value in a named_scope.

Don't use a named scope; create an anonymous scope dynamically.

class ApplicationController
def students(staff_id = session[:staff_id])
Student.scoped:)conditions => {:staff_id => staff_id})
end
end

@students = students.find(...)

Or you can make a helper method using with_scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top