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

  • SEARCH
  • Home
  • 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 3426588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:44:02+00:00 2026-05-18T06:44:02+00:00

My method loads a list of countries(code, name) into the database, but before that

  • 0

My method loads a list of countries(code, name) into the database, but before that it has to check, if the country data does not already exist. This works fine:

 def self.load_countries
    get_countries.each do |country|
      code, name = country
      if find_by_code(code).nil?
        create({ 'name' => name, 'code' => code })
      end
    end
  end

However, as I am new to Ruby, I want to learn the best practises. So, in this code I am not sure about two things which might be (or might not be) optimised:

  1. find_by_attribute returns the “select * from table” statement. In this case, when I don’t need any data from database – I just want to know if the record exists or not – selecting the entire row seems a little inefficient to me. Is there any better way solve this? For example, “select 1 from table where …” using ActiveRecord?
  2. This question might be silly, but I want to be sure: when I start the loop with the get_countries.each, is it ok to use a method instead of a variable? Isn’t the same method called each cycle (N times)? In other words, would this be anymore efficient:

    countries = get_countries

    countries.each do |country|

Any comments on those few lines of code are welcome, since the fact it works doesn’t necessarily mean that I am doing it the right way.

Thank you.

  • 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-18T06:44:03+00:00Added an answer on May 18, 2026 at 6:44 am

    You can use the exists? function in ActiveRecord.

    def self.load_countries
      get_countries.each do |country|
        code, name = country
        unless exists?(:code => code)
          create({ :name => name, :code => code })
        end
      end
    end
    

    The get_countries function gets called only once. It returns an enumerable data type and then the each walks through each of them.

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

Sidebar

Related Questions

I need to call a method that accepts a stream argument. The method loads
What method do you use when you want to get performance data about specific
What method do I call to get the name of a class?
I have an application with one form in it, and on the Load method
I'm trying to load spring beans using XmlWebApplicationContext setConfigLocations method. However, I keep getting
Is there any method? My computer is AMD64. ::std::string str; BOOL loadU(const wchar_t* lpszPathName,
Method chaining is the only way I know to build fluent interfaces. Here's an
Every method I write to encode a string in Java using 3DES can't be
The method signature of a Java main method is: public static void main(String[] args)
Which method is preferred? Session.Remove(foo); Session[foo] = null; Is there a difference?

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.