简体   繁体   中英

Add data from one table to another in ruby on rails

In my current project, I have to get some data in a column of one table and put them to the 2nd table. The first table data have been saved as hash as follows:

--- 
- werweqr
- test
- B1
- B2
- B3
- xvxczv

I write the following code in the migration file to add the data from the first table to the 2nd table. But the data are not sending from the first to second.

 @scenario_response = ScenarioResponse.where("selected_barriers != ?", "");
    @scenario_response.each do |p|
      p.selected_barriers.each do |barrier|
        Settings.test = barrier
        # SelectedBarriers.create(:scenario_response_id => p.id, :barrier => barrier)

      end
    end

Can anyone please let me know if there's something wrong in my code.

If so how to fix it?

Thanks a lot

I don't think you need to call "each" on p.selected_barriers.Try removing the each and doing this: Settings.test=p.selected_barriers . I'm new to RoR too..According to me,the scenario_response is a collection that returns all instances that have the selected_barriers as "". Since you are doing an each on the collection, you will just have one selected_barriers item for each of them. Please try this and let me know if I'm wrong.

Also you are not doing update_attributes. Try doing Settings.update_attributes(params[:test]) after Settings.test = barrier .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM