根据current_user动态设置named_scope

发布于 2024-08-18 21:49:33 字数 1610 浏览 6 评论 0原文

我不断收到以下错误:

当你没有的时候你有一个nil对象 期待吧!您可能期望 数组的实例。发生错误 在评估 nil.size 时

,当他们导航到页面时,我想将他们限制为他们可以根据其网站看到的内容。问题是站点和用户表之间没有直接关联。联系人有一个用户(用户信息存储在 current_user 变量中)。一个网站有很多联系人。一个站点 has_many Students,其中 Students 表的外键为 site_id。因此,学生和网站之间存在链接,因此当当前用户导航到学生页面时,他们只能看到与他们相同网站的学生。我可以通过在命名范围中硬编码一个数字来仅显示当前用户站点的学生来实现此目的。但不同的用户将属于不同的站点,因此登录后,与其关联的站点将会改变。这就是问题所在 - 在named_scope中动态设置该值。这就是我所拥有的:

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

学生模型

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

感谢您的任何建议。

表之间的关系:

users: contact_id 联系人:主键、contactable_id、contactable_type 站点:主键 学生:site_id

用户模型 own_to :contact

联系人模型 有_一个:用户 属于:可联系,:多态=>正确,:依赖 => :销毁

站点模型 has_many :联系人, :as => :可联系 has_many :students

学生模型 belongs_to :site

这成功地按站点限制了学生: 学生控制器 def 索引范围 如果 current_user.role_id == 8 super.site_staff_limit 别的 超级.with_state.with_site 结尾 最终

学生模型 命名范围:site_staff_limit,:条件=> {:site_id =>; 1}

问题是不同的用户将属于不同的站点,因此他们只能访问其所属站点的学生记录。我很难使上面的named_scope足够动态来完成此任务。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-08-25 21:49:33

您也许可以通过 :through 关系在用户和站点之间设置链接。

您提供的代码有效吗?
是否出现错误?

You might be able to set up a link between users and sites, with a :through relationship.

Does the code you have provided work?
Is there an error that occurs?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文