Dynamically set named_scope based on current_user

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.

···

--
Posted via http://www.ruby-forum.com/.

John Merlino wrote:

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.

···

--
Posted via http://www.ruby-forum.com/\.

John Merlino wrote:

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.

···

--
Posted via http://www.ruby-forum.com/\.

Since I suspect this is a Rails app, you will have better success when asking the Rails community:

···

On 27.01.2010 00:52, John Merlino wrote:

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.

--
Phillip Gawlowski