3

In my form i have a lot of fields, and when i submit my form im my url i see a lot of empty params, like url?a=&b=&c= and my has_scope model think that i want to use this params, but with null value, but this is wrong.

Part of my model and controller:

class CarsController < ApplicationController
 has_scope :by_manufacturer
 has_scope :by_price, :using => [:price_from, :price_to]
end

class Car < ActiveRecord::Base
 scope :by_manufacturer, -> vehicle_manufacturer_id { where(:vehicle_manufacturer_id => vehicle_manufacturer_id) }
 scope :by_price, -> price_from, price_to { where("price >= ? AND price <= ?", price_from, price_to) }
end

How could i write something like:

if vehicle_manufacturer_id.present? 
 has_scope :by_manufacturer
end

how is it right to check on field presense? And where to write in and how?

infused
  • 24,000
  • 13
  • 68
  • 78
brabertaser19
  • 5,678
  • 16
  • 78
  • 184

1 Answers1

2

Has_scope has an :if condition you can use to determine when the scope should be used. Example:

has_scope :by_manufacturer, if: vehicle_manufacturer_id.present?

Or add an condition to the scope itself:

scope :by_manufacturer, -> vehicle_manufacturer_id { where(:vehicle_manufacturer_id => vehicle_manufacturer_id) if vehicle_manufacturer_id.present? }

I cannot test it right now, but this should work. However, I don't think your problem is wether or not you call the scope. The URL params are passed on from your view to your controller. The scope only determines what records return given certain conditions, they say nothing about what URL params should be shown.

rails4guides.com
  • 1,441
  • 2
  • 11
  • 8
  • how to use compact with has_scope: – brabertaser19 Mar 29 '14 at 08:25
  • sorry for my attention... but how to use compact with has_scope? – brabertaser19 Mar 29 '14 at 08:49
  • ok, seems to work... just how: `scope :by_price, -> price_from, price_to { where("price >= ? AND price <= ?", price_from, price_to) if (!price_from.nil? && !price_to.nil?)}` to be if only from or only to price is entered? maybe one more has_scope for each: from or to price? – brabertaser19 Mar 29 '14 at 09:39
  • What is the problem? You use &&, so the scope will not be called if either the price_from or price_to is not present. – rails4guides.com Mar 29 '14 at 09:43
  • could be such situation: user entered only price_from or only price_to in db i must search! just with other condition – brabertaser19 Mar 29 '14 at 09:50
  • I think I understand you now. You also want to search the DB if only the price_from or price_to is set? Then you could use 2 scopes indeed. Example: scope :price_from, ->(price_from){ where('price >= ?', price_from) if price_from.present? } – rails4guides.com Mar 29 '14 at 09:54
  • ok, thank you, that is what i want to hear... Also for some reasons `scope :by_manufacturer, -> vehicle_manufacturer_id { where(:vehicle_manufacturer_id => vehicle_manufacturer_id) if !vehicle_manufacturer_id.nil?}` - condition work, but `scope :by_price, -> price_from, price_to { where("price >= ? AND price <= ?", price_from, price_to) if !price_from.nil? && !price_to.nil?}` didn't work, maybe you know why? – brabertaser19 Mar 29 '14 at 09:56
  • I wouldn't use !price_from.nil? but use price_from.present? instead. Because if you use !price_from.nil? you are still allowing blank ('') values. So you would get: (price_from.present? && price_to.present?). Anyway, if you use 2 separate queries for price_from and price_to you don't need the by_price scope at all. You can just chain the scopes, as in: collection.price_from.price_to – rails4guides.com Mar 29 '14 at 09:58
  • "you use 2 separate queries for price_from and price_to you don't need the by_price scope at all. You can just chain the scopes, as in: collection.price_from.price_to –" didn't understand, maybe code? also i accept... – brabertaser19 Mar 29 '14 at 10:01
  • If you have scope :price_from, ->(price_from){ where('price >= ?', price_from) if price_from.present? } and scope :price_to, ->(price_to){ where('price >= ?', price_to) if price_to.present? } you can call has_scope for the price_from and the price_to. If both scopes are called this would return records where the price is between price_from and price_to. The 2 separate scopes together do the same as your by_price scope. Is it more clear now? P.s. Can you remove the 1 sentence comments above, to clean up the comments section? – rails4guides.com Mar 29 '14 at 10:11