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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:33:30+00:00 2026-05-26T19:33:30+00:00

I need some help in thinking through the process to do batch update on

  • 0

I need some help in thinking through the process to do batch update on multiple tables for RoR application. Following are my models,

class User < ActiveRecord::Base  
    has_many :addresses  
    has_many :phones  
end  


class Address < ActiveRecord::Base  
    belongs_to :user  
    has_one    :addresstype      
end  


class Phone < ActiveRecord::Base  
    belongs_to :user  
    has_one    :phonetype  
end  


class PhoneType < ActiveRecord::Base  
    belongs_to :phone  
end  


class AddressType < ActiveRecord::Base      
    belongs_to :address    
end    

You can imagine “Address” table has user_id, PhoneType has phone_id, and AddressType has address_id keys to maintain associations.

So, I want to process some files with user records to insert into appropriate tables. For e.g.

...  
    usr1@foo.com,1234 sw main st. ca 19820,offce,425-378-1188,mobile  
    usr1@foo.com,7869 sw fool st. ca 19820,residential,425-898-2345,landline  
    usr2@foo.com,4321 sw oak st. ca 19822,offce,435-378-1298,mobile  
    usr3@foo.com,8789 sw adler st. ca 19822,residential,436-898-6234,landline  
...

millions of them either all in one file or one record per file transfered from remote server.

OR, is there any other way to process these remote requests on demand basis? For e.g. remote servers send a record to my RoR application and it gets processed thru RoR app.
In both cases I want to make sure data to be inserted passes all validation rules. Like email format is valid, or address can not be empty.

These records could be in json format to save on size of data to be transfered in a file.

While processing user (usr1@foo.com) may or may no exists.

Thanks and I really appreciate any help.

Atarang.

  • 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-26T19:33:31+00:00Added an answer on May 26, 2026 at 7:33 pm

    You can do both. If you have one file you can simply create a ruby script and include config/environment.rb to load the environment of the rails application in your ruby script. Then you can load the file and deal with each line like this:

    include 'config/environment.rb'
    File.open("/folder/folder/file_to_parse", "r").each_line do |line| 
         .....
         # interpret line here
         ....
    end
    

    If its in CSV format you can also use rubys CSV Librarie (http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html)

    On the other hand you could also create an interface that parses the data in JSON. You could use the JSON implementation for ruby (which is documentated here: http://flori.github.com/json/) to create an action that parses a single object thats transfered in JSON.

    //About the validations:

    Write validations in the Models! Then you check all the Models that belongs_to sth. because you may already have the PhoneType thats used in the actual record. So care about not getting duplicaties when you dont need them.

    After that you use the new method to create new objects (not create because create writes to the database). Then you parse the line and when the line is parsed you can check all your objects using the valid? method which checks if the object passes all the validations defined in the model.

    And only if all your objects pass the validation you safe them to the database. Otherway you simply dont execute the save method so datasets which dont pass the vaidations arent stored in the databse.

    //About th eperformende

    If you just have to perform the import once its not that big problem if the script isnt that perfect. What you should do when there are many entries in the table (100 arent many, 100000 are,..) you should add indexes to all the foreign key columns. You can create a migration addIndexesToForeignKeys and add indexes with the add_index method like this:

    add_index :table_name, :column_name
    

    This will speed up select queries and slow down insert queries a bit but I think show actions will be called more often then create actions 😉

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

Sidebar

Related Questions

Need some help about with Memcache. I have created a class and want to
Hi guys I need some help here, to my bad thinking brain : I
I need some help in picking the 'right' solution for the following 'problem'. I
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help assigning a mouseover event to display some icons that start out
I need some help from the shell-script gurus out there. I have a .txt

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.