通过伪属性搜索 ActiveRecord
ActiveRecord searching by pseudo attributes
在我的模型上我有方法
def complete?
self.address_id && self.user_id && self.location
end
是否可以使用 activerecord 搜索符合此条件的记录,或者我是否必须在每次查找中检查所有这些列是否为 nil?
看起来这样做的方法是为模型创建一个范围:
scope :complete, -> { where.not(address_id: nil, user_id: nil, location_id: nil)}
然后用Model.complete
搜索
在我的模型上我有方法
def complete?
self.address_id && self.user_id && self.location
end
是否可以使用 activerecord 搜索符合此条件的记录,或者我是否必须在每次查找中检查所有这些列是否为 nil?
看起来这样做的方法是为模型创建一个范围:
scope :complete, -> { where.not(address_id: nil, user_id: nil, location_id: nil)}
然后用Model.complete