I have a long loop that results in this:
csv_code = CSV.generate do |csv|
csv << ["Product ID","Name", "Url"]
@all_products.each do |product|
if product.page_url("en_US") != nil
turl = product.page_url("en_US")
end
csv << [product.name,product.d_id, turl]
end
end
The method uses products 1-17 works great resulting in a url printed. When I get to my 18th record I have problems
Product.find(18) // product found!
product.find(18).page_url("en_US")
NoMethodError: undefined method `page_url' for nil:NilClass
How can I protect against these undefined events?
url = product.page_url(“en_US”)
The issue is that a
productisnil:(It has nothing to do with
page_urlmaybe returningnil.)Make sure
productcan’t benil: but be wary that this may be a deeper issue. In any case, “fixing” this issue is easy to deal with.Consider either using a collection restriction (such as Enumerable#reject):
The above uses the Symbol#to_proc “Rails magic”, but could just as easily have been
{|x| x.nil?}as the restriction. The downside is it’s not practical to use this for a “no URL” condition per-product although Enumerable#partition could help with that: use the right tool for the job.Another solution is to expand the conditional check itself:
The short-circuit nature of
&&will ensurepage_urlis only invoked upon a truthy value (which excludesnil).I also took the liberty of assuming
page_urlcan’t returnfalseas I find this makes the intent more clear.Happy coding.