0
我有一个Filter类用于过滤转租列表。在我的过滤器类中,我有一个Listing方法,该方法返回符合用户在表单中输入的条件的所有列表。这是过滤器类。Rails查询数组是否包含其他数组
class Filter < ActiveRecord::Base
attr_accessible :air_conditioning, :available_rooms, :bathrooms, :furnished, :negotiable, :new, :parking, :maximum_price, :private_bathroom, :show, :term, :total_rooms, :utilities, :washer_dryer
serialize :term
def listings
@listings ||=find_listings
end
private
def find_listings
listings=Listing.order(:price)
listings=listings.where("listings.price <= ?", maximum_price) if maximum_price.present?
listings=listings.where(total_rooms: total_rooms) if total_rooms.present?
listings=listings.where(available_rooms: available_rooms) if available_rooms.present?
listings=listings.where(bathrooms: bathrooms) if bathrooms.present?
listings=listings.where(term: term)
listings=listings.where(furnished: furnished)
listings=listings.where(negotiable: negotiable)
listings=listings.where(utilities: utilities)
listings=listings.where(air_conditioning: air_conditioning)
listings=listings.where(parking: parking)
listings=listings.where(washer_dryer: washer_dryer)
listings=listings.where(private_bathroom: private_bathroom)
listings
end
end
有问题是行
listings=listings.where(term: term)
期限保存为可序列化的字符串。 Aka ['Summer','Fall]用户选中复选框以选择可以使用哪些条款。但是,where子句每次都失败。如何检查用户选择的元素是否包含在列表条款中?
这不是一个查询,虽然 – 2013-03-03 21:42:01
你是对的,这不是一个查询。当您尝试运行您发布的代码时,您会得到什么错误?列表模式是什么样的?由于它是Ruby对象的序列化字段,因此您可能无法在类似的SQL查询中使用它。但是你可以在从数据库中得到结果后进行过滤,例如: listings.find_all {| l | l.include? term} – Ari 2013-03-03 21:46:51