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 7785431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:15:17+00:00 2026-06-01T20:15:17+00:00

I have rake tasks importing thousands of records from several different sources and formats

  • 0

I have rake tasks importing thousands of records from several different sources and formats and I’m looking to dry up my code after the parsing, where they currently create or update model records using find_or_initialize_by_* dynamic finders.

Essentially, I’d like to be able to pass in the * section of the find_or_initialize_by_* method.

Here is some sudo code to try explain what I want to achieve.

def create_or_update_record(*args)
  model = args[0].classify.constantize
  identifier = args[1]
  attributes = args.extract_options!

  XXX = identifier

  record = model.find_or_initialize_by_XXX(identifier.to_sym => @identifier_value)

  attributes.each do |attribute|
   #set value of attribute here
  end

  record.save
end

Which I would then call from the rake tasks with something like this in the Product import…

  create_or_update_record('Product', 'product_id',{ 
    "product_id" => "1",
    "product_price" => "2.99"
  })

and something like this in the Category import…

  create_or_update_record('Category', 'category_id',{ 
    "category_id" => "1",
    "category_name" => "Gloves"
  })

I’m guessing I need to override and extend the underlying method_missing. Looks pretty complicated from this blog post I found. http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work

  • 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-06-01T20:15:18+00:00Added an answer on June 1, 2026 at 8:15 pm

    Something like this would work:

    To create

    #record = model.find_or_initialize_by_XXX(identifier.to_sym => @identifier_value)
    

    We’ll send the dynamic finder to the object, and use the existing method missing

    identifier = "XXX"
    record = model.send("find_or_initialize_by_#{identifier}", identifier.to_sym => @identifier_value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple rake task importing records from a CSV file and saving
I have several rake tasks combined into one rake command. Just wondering is it
I have an app built on Sinatra. It's supported by several rake tasks that
I have rake tasks for getting the production database from the remote server, etc.
I have this in lib/tasks/foo.rake : Rake::Task[assets:precompile].enhance do print >>>>>>>> hello from precompile end
I have some rake tasks that run each minute and of course that spawns
I have rake tasks which i want to run in proper sequence. I want
I have two simple rake tasks -- one that works and one that throws
I have a simple rails application with a few controller and some rake tasks.
I have seen some apps that have a few rake tasks included to load

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.