English 中文(简体)
根据铁路的出入控制规则,如何以最佳方式确定范围?
原标题:What is the best way to do scoped finds based on access control rules in Rails?

我需要在出入控制规则的基础上,找到一种对所发现的面积有利的解决办法。 基本上,我有以下机构:

Users Customers AccessControl - Defines which user has access to another users data

用户不仅需要能够接触自己的客户,而且还需要与其他用户共享客户。

很显然,像一个简单的协会这样的东西不会奏效:

has_many :customers

并且不会:

has_many :customers, :conditions =>  user_id in (1,2,3,4,5) 

由于协会使用“范围”和“附加条件”是不是“或”条件。

此外,我还试图推翻这一发现和办法。

has_many :customers do
  def find(*args)
    #get the user_id and retrieve access conditions based on the id
    #do a find based on the access conditions and passed args
  end 

  def method_missing(*args)
    #get the user_id and retrieve access conditions based on the id
    #do a find based on the access conditions and passed args
  end
end

但问题是,在推广方法中,我没有接触用户物体/母物体,而只是没有按计划工作。

我也尝试了违约——范围,但正如在座,然后你可以把一个障碍变成一个违约范围。

无论如何,我知道,在使用铁路之前就进行了数据分割和数据存取控制,并想知道有些人是否找到了这样做的有利途径。

UPDATE: The AccessControl table has the following layout

user_id
shared_user_id

客户表有这种结构:

id
account_id
user_id
first_name
last_name

将把以下数据列入“出入控制表”:

1  1
1  3
1  4
2  2
2  13
and so on...

用户1的账户为13个,我需要能够检索客户,最好用以下图表说明:

select * from customers where (account_id = 13 and user_id = null) or (user_id in (1,3,4))
问题回答

如果我完全错过这里的话,我不相信你想要做的事情。 用户与客户之间的关系吗? 如果这样看,你就迫切需要建立一种多管关系。

class User
  has_and_belongs_to_many :customers   

  # or this if you need to store meta data in the join table 

  has_many :customers
  has_many :access_controls
  has_many :accessible_customers, through => :access_controls
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签