0

This is what I'd like to do:

I have this piece of code:

customer = Customer.find(:first, :conditions => {:siteId => params[:siteId], :customerCode => params[:id]})

If :customerCode is null, I'd like to use :temporaryCode instead. But I don't know how. Thanks in advance.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Aldrin Dela Cruz
  • 205
  • 1
  • 4
  • 15
  • 1
    `if (params[:id])` to check to param `id` exists or not. – xdazz Jun 04 '12 at 05:36
  • you use params[:id] in customercode if params id is null then what would you use instead of params[:id] for temporarycode? – urjit on rails Jun 04 '12 at 05:44
  • @xdazz: `params.has_key? :id` would be better since that is exactly what you mean. – mu is too short Jun 04 '12 at 05:45
  • @muistooshort Won't `if (params[:id])` be enough in most situations? – xdazz Jun 04 '12 at 05:47
  • @xdazz: There is a possibility that someone is preprocessing `params` so `nil` could be a valid value for `params[:id]`. I think it is best to say exactly what you mean rather than making assumptions. – mu is too short Jun 04 '12 at 05:50
  • @muistooshort Yep, You're right :) – xdazz Jun 04 '12 at 05:53
  • @muistooshort can you elaborate further about params.has_key? :id? I'm new to that concept. Thank you. If you can give me an example that would really be great. – Aldrin Dela Cruz Jun 04 '12 at 06:04
  • [`Hash#has_key?`](http://ruby-doc.org/core-1.9.3/Hash.html#method-i-has_key-3F) returns `true` if the Hash has the key you're asking about and `false` otherwise. I don't think that's what you're looking for here though, you seem to want to use a different column in the database if `customercode` is NULL, you'd use `has_key?` to ask `params` if it contains a certain key. – mu is too short Jun 04 '12 at 06:23
  • @xdazz: I think they want to use `:customerCode` if that column isn't NULL and `:temporaryCode` if `:customerCode` is NULL. – mu is too short Jun 04 '12 at 06:25
  • @muistooshort yes, actually that's what i'm really looking for. I'm testing the answer you gave me right now – Aldrin Dela Cruz Jun 04 '12 at 06:58

2 Answers2

1
customer   = Customer.find_by_siteid_and_customercode  params[:siteId], params[:id]
customer ||= Customer.find_by_siteid_and_temporarycode params[:siteId], params[:id]

making use of finders is most safer, cleaner

Amol Pujari
  • 2,280
  • 20
  • 42
0

I'm pretty sure you want to use COALESCE inside the database:

customer = Customer.where(:siteId => params[:siteId])
                   .where('coalesce(customercode, temporarycode) = ?', params[:id])
                   .first

The SQL COALESCE function returns the first of its arguments that isn't NULL. For example:

coalesce(column1, column2)

gives you column1 if column1 isn't NULL and column2 if column1 is NULL.


If you're using Rails2 then something like this should work:

Customer.find(:first, :conditions => [
    'siteid = ? and coalesce(customercode, temporarycode) = ?',
    params[:siteId], params[:id]
])
mu is too short
  • 426,620
  • 70
  • 833
  • 800