Forum: Ruby on Rails Query help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-02-02 13:58
Hi..
Any ways to optimise this conditions using ternery operator

if params[:userinfo]
  if params[:userinfo][:company_id] == nil or
params[:userinfo][:company_id]
      == "Select Company"
    company_id = nil
  else
    company_id = params[:userinfo][:company_id]

    end
  if params[:userinfo][:department_id] == nil or
params[:userinfo][:department_id] == "Select Department" or
params[:userinfo][:department_id] == "Any Department"
            department_id = nil
          else
            department_id = params[:userinfo][:department_id]
          end
          if params[:userinfo][:role_id] == nil or
params[:userinfo][:role_id] == "Select Role" or
params[:userinfo][:role_id] == "Any Role"
            role_id = nil
           puts "here"
          else
            role_id = params[:userinfo][:role_id]
            puts "els"
          end
           online_status = params[:userinfo][:online_status]

end
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-02 20:43
(Received via mailing list)
Newb Newb wrote:
> Hi..
> Any ways to optimise this conditions using ternery operator
>
> if params[:userinfo]
>   if params[:userinfo][:company_id] == nil or
> params[:userinfo][:company_id]
>       == "Select Company"

As the first of a great many minor issues with this code, you can DRY it
by
stashing each parameter as you 'if' it...

   if userinfo = params[:userinfo]
     if (company_id = userinfo[:company_id]) == nil or
              company_id == 'Select Company'

...but there are also major issues. Your <select> options should use an
id in
their value='' attributes, and the one for 'Select Company' should be
nil...
This topic is locked and can not be replied to.