0
   object.each do |info|
     @user_work_history.fb_user_id = facebook_session.user.id
     @user_work_history.city = info.location.city
     @user_work_history.country = info.location.state
     @user_work_history.company_name = info.company_name
     @user_work_history.description = info.description
     @user_work_history.start_date = info.start_date
     @user_work_history.end_date = info.end_date
     @user_work_history.position = info.position
     @user_work_history.updated_at = Time.now
     @user_work_history.save
   end

here is the code but inside loop data is not saving only one record is saved.

0

2 Answers 2

2

I believe you have to add

@user_work_history = UserWorkHistory.new

to the first line inside the loop. (or whatever the model is)

Sign up to request clarification or add additional context in comments.

Comments

2

When you do that kind of loop you need to change the reference to the instance variable @user_work_history. What your loop does is that for each iteration it updates the state of the same object!

What you should do, is to set the @user_work_history before each iteration, like this:

object.each do |info|
     @user_work_history = UserWorkHistory.new(:fb_user_id => facebook_session.user.id) # for example
     @user_work_history.city = info.location.city
     @user_work_history.country = info.location.state
     (...)
     @user_work_history.save # here it saves new tuple
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.