我想在rails active record中实现类似的功能。
SELECT *
FROM tires
JOIN comparison_prices
WHERE comparison_prices.simple_tire_price < tires.web_price
AND comparison_prices.simple_tire_price >= tires.web_price - 5
目前我有这样的事情:
@tires = Tire.all
@tires = @tires.joins(:comparison_price).where('tires.web_price > 0')
@tires = @tires.paginate(page: params[:page], per_page: 10).order(:id)
答案 0 :(得分:0)
因为您的Tire
模型has_one :comparison_price
可以写:
@tires = Tire.joins(:comparison_price)
.where('comparison_prices.simple_tire_price < tires.web_price')
.where('comparison_prices.simple_tire_price >= tires.web_price - 5')
.paginate(page: params[:page], per_page: 10).order(:id)
或者:
@tires = Tire.joins(:comparison_price)
.where('comparison_prices.simple_tire_price BETWEEN tires.web_price - 5 AND tires.web_price')
.paginate(page: params[:page], per_page: 10).order(:id)