Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:30:22+00:00 2026-05-25T15:30:22+00:00

I have a long loop that results in this: csv_code = CSV.generate do |csv|

  • 0

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”)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:30:23+00:00Added an answer on May 25, 2026 at 3:30 pm

    The issue is that a product is nil:

    undefined method ‘page_url’ for nil:NilClass”. Solution:

    (It has nothing to do with page_url maybe returning nil.)

    Make sure product can’t be nil: 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):

    @all_products.reject(&:nil?).each do {
       ...
    }
    

    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:

    if product && product.page_url("en_US")
       # yay
    else
       # uhm
    end
    

    The short-circuit nature of && will ensure page_url is only invoked upon a truthy value (which excludes nil).

    I also took the liberty of assuming page_url can’t return false as I find this makes the intent more clear.

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long URL with tons of parameters that I want to open
I have long tables generated by datagrid control that go beyond the page width.
I have long recognized that any set of whitespace in an HTML file will
I have long been of the opinion that the time and resources it would
I have a long running SQL statement that I want to run, and no
I have a long running process in VB6 that I want to finish before
I have several long running report type transactions that take 5-10 minutes. Would I
I'm performing a nested loop in python that is included below. This serves as
I have a file like this: 1234 1234 First Name Last Name And I
I have a simple while loop that checks the database for inserts. If its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.