我正在构建房东mgt应用程序。但是我遇到了一个问题-我只想索引house.id
(关联值)与当前用户匹配的请求。
当前,我已经根据用户是否创建了它来工作了。但我希望房东能看到与其财产有关的所有要求。
requests_controller.rb:
def index
unless current_user.estate_agent?
@requests = Request.where(user_id: current_user).search(params[:search])
else
@requests = Request.search(params[:search])
end
end
request.rb
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :house
def self.search(search)
key = "%#{search}%"
if search
where('description LIKE ? OR title LIKE ?', key, key)
else
all
end
end
end
house.rb
class House < ActiveRecord::Base
belongs_to :user
belongs_to :tenant
has_many :requests
belongs_to :contract
mount_uploader :energy, EnergyUploader
validates_presence_of :house_title, :description, :doorno, :postcode, :price, :bedroom, :house_type
def full_house_name
"#{doorno} #{house_title}"
end
def self.search(search)
key = "%#{search}%"
if search
where('doorno LIKE ? OR house_title LIKE ? OR price LIKE ? OR postcode LIKE ? OR house_type LIKE ? OR bedroom LIKE ?', key, key, key, key, key, key)
else
all
end
end
end
答案 0 :(得分:0)
如果我对您的理解正确,则希望显示用户提出的或针对其真实情况提出的所有请求。
下面的代码将创建您的初始过滤器,然后您可以在其上调用search
方法。
Request.where(user_id: current_user.id).or(Request.where(house: { user_id: current_user.id}))