Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails是通过作用域连接多个表的表_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails是通过作用域连接多个表的表

Ruby on rails Rails是通过作用域连接多个表的表,ruby-on-rails,Ruby On Rails,我正在按角色对页面进行筛选。因此,如果用户是超级管理员,则通过两个表显示其角色匹配的其他所有内容 用户示例: 用户:约翰·史密斯 角色:太平洋西北 区域示例: 地区:太平洋西北部 地点:西雅图、波特兰、博伊西 如果John登录到see events,他应该只查看那些位于该区域内的事件。假设波特兰有一场比赛,约翰就会看到 用户有许多角色,地区有许多位置。事件属于位置和区域,但可选 现在,这适用于将用户角色连接到区域: scope :for_user, lambda { |user| if

我正在按角色对页面进行筛选。因此,如果用户是超级管理员,则通过两个表显示其角色匹配的其他所有内容

用户示例:

  • 用户:约翰·史密斯
  • 角色:太平洋西北
区域示例:

  • 地区:太平洋西北部
  • 地点:西雅图、波特兰、博伊西
如果John登录到see events,他应该只查看那些位于该区域内的事件。假设波特兰有一场比赛,约翰就会看到

用户有许多角色,地区有许多位置。事件属于位置和区域,但可选

现在,这适用于将用户角色连接到区域:

scope :for_user, lambda { |user|
 if user.super_admin?
  self
 else
  joins(:region)
   .where(Region.arel_table[:name].matches("%#{user&.role&.name}%"))
 end
}
所以Location.name,Location.region\u id=region.id,region.name=Role.name,Role.id=User.Role\u id

我想我可以试试这样的东西:

joins(:location)
 .where(Location.arel_table[:region_id]).matches("%#{region.id}")
 .joins(:region)
 .where(Region.arel_table[:name].matches("%#{user&.role&.name}%"))
然而,这就产生了:

不支持的参数类型:# 因此,假定可排序表具有位置和区域。我如何根据与位置名称匹配的用户角色筛选

编辑:

我想我可以用以下方法来打开它:

region = Region.find_by(name: user&.role&.name)
if region.present?
 joins(:location)
  .where(location_id: region.location_ids)
end
然而,这显示了所有位置。我觉得有点接近了。于是我试着:

region = Region.find_by(name: user&.role&.name)
if region.present?
 location = region.where(location_id: region.location_ids)
 return true if location.present?
end
失败了,所以我试着:

region = Region.find_by(name: user&.role&.name)
list_locations = []
Location.all.each.do |loc|
 list_locations << loc.name if loc.region_id == region.id && region.present?
end
self if list_location.present?
region=region.find_by(名称:user&.role&.name)
列出位置=[]
Location.all.each.do | loc|

列出位置将此项保留在此处,以防对某人有所帮助:

region = Region.find_by(name: user&.role&.name)
if region.present?
 joins(:location)
  .where(location_id: region.location_ids)
end
这确实奏效了。我更新了我的视图以显示位置、区域和名称,并注意到它正在过滤,而且我的页面太多,似乎无法正常工作