如何使用和将活动记录查询写入详细信息

How to write active record query to details by using and

我有两个 table

1)属性:字段为id,name,propert_type,category_id

2) 管理员:字段 ID、姓名、手机,category_id

我想写一个活动记录来列出所有属性,其中属性 table 中的 category_id 和管理员 table 中的 category_id 相等,根据 current_user_id

我通过以管理员身份登录来列出此 属性 列表。

模型关系

class Category < ActiveRecord::Base has_many :admins,dependent: :destroy has_many :properties,dependent: :destroy end

class Admin < ActiveRecord::Base belongs_to :category has_many :properties end

class Property < ActiveRecord::Base belongs_to :admin belongs_to :category end

我这样写活动记录,但是我得到了错误, 谁能给我建议一个解决方案

@properties= Property.where('properties.category_id=?','admins.category_id=?').and('admins.id=?',current_user.specific.id)

与您的关联,您可以使用子查询在一行中获取结果

@properties = Property.where(category_id: Admin.select("category_id").where(id: current_user.id))

根据我的理解 current_user 是一个 Admin。所以您可以通过 current_usercategory_id 进行搜索。如果我是对的,试试这个

@properties = Property.where(category_id: current_user.category_id)