简体   繁体   中英

Is there a more ruby way of doing this

Ok so i have this helper

def current_company_title
 (Company.find_by_id(params["company_id"]).name rescue nil) || (@companies.first.name rescue nil) current_user.company.name
end

Basically what I am achieving with this is the following ...

If the param["company_id"] exists then try to get the company and if not then if @companies exists grab the first company name and if not then get the current users company name

This works but the rescues seem like a hack...any idea on another way to achieve this

Indeed rescue is kind of a hack, id' probably split it up into two methods and then use try to fetch the name if available: http://api.rubyonrails.org/classes/Object.html#method-i-try

def current_company
  @current_company ||= Company.find_by_id(params[:company_id]) || @companies.try(:first) || current_user.try(:company)
end

def current_company_name
  current_company.try(:name)
end

Less "magic", simple code, simple to read:

def current_company_title
 company = Company.where(id: params["company_id"]).presence
 company ||= @companies.try(:first)
 company ||= current_user.company
 company.name
end

Ps. Not a big fan of Rails' try method, but it solves the problem.

Company.find_by_id(params["company_id"]).name`

find and its derivates are meant to be used when you're sure-ish you'll have a positive result, and only in some cases (row was deleted, etc) errors. That's why it raises an exception. In your case, you're assuming it's gonna fail, so a regular where , which would return nil if no rows was found, would do better, and remove the first rescue

@companies.first.name rescue nil

could be replaced by

@companies.first.try(:name)

I'll let you check the api for more on the topic of try . It's not regular ruby, it's a Rails addition.

def current_company_title
    if params["company_id"]
        return Company.find_by_id(params["company_id"]).name
    elsif @companies
        return @companies.first.name
    else
        return current_user.company.name
    end
end

The rescues are a hack, and will obscure other errors if they occur.

Try this:

(Company.find_by_id(params["company_id"].name if Company.exists?(params["company_id"]) ||
(@companies.first.name if @companies && @companies.first) || 
current_user.company.name

then you can extract each of the bracketed conditions to their own methods to make it more readable, and easier to tweak the conditions:

company_name_from_id(params["company_id"]) || name_from_first_in_collection(@companies) || current_user_company_name

def company_name_from_id(company_id)
  company=Company.find_by_id(company_id)
  company.name if company
end

def name_from_first_in_collection(companies)
  companies.first.name if companies && companies.first
end

def current_user_company_name
  current_user.company.name if current_user.company
end
[Company.find_by_id(params["company_id"]),
  @companies.to_a.first,
  current_user.company
].compact.first.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM