1

I have parameters like this:

    Parameters: {
"map"=>[
{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, 
{"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, 
{"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}
]}

and strong params defined:

def map_params
  params.permit( map: [:lat,:lon, :vibration_level, :time_sent])
end

How to save all of my 3 objects in rails controller method. I can do that for 1 object but not for 3.

EDIT: for saving one object I use:

params.require(:map).permit(:lat, :lon, :vibration_level, :time_sent)

But, I guess this wouldn't work for multiple objects.

Viktor
  • 2,623
  • 3
  • 19
  • 28
yerassyl
  • 2,958
  • 6
  • 42
  • 68

1 Answers1

1

This is the basic way to create multiple record at a time with rails,

  maps = Map.create([
              {"lat"=>"51.088672", "lon"=>"71.396522", vibration_level"=>"300", "time_sent"=>"07:25:00"}, 
              {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"},
              {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}
  ])

You can use your map_params for creating all maps at time as following

maps = Map.create(map_params["map"])
Rokibul Hasan
  • 4,078
  • 2
  • 19
  • 30
  • Looks, like nothing happens. (0.1ms) begin transaction (0.1ms) rollback transaction – yerassyl Jun 14 '15 at 19:38
  • trace what is return by `map_params`, data format should be as in above example, if not re-write your `def map_params`. – Rokibul Hasan Jun 14 '15 at 19:39
  • sorry i am a new to rails, how can i trace? – yerassyl Jun 14 '15 at 19:40
  • past this `puts map_params` before `Map.create(map_params)`, and observer development log. – Rokibul Hasan Jun 14 '15 at 19:42
  • get my params printed out. – yerassyl Jun 14 '15 at 19:44
  • whats that ? Past here please. – Rokibul Hasan Jun 14 '15 at 19:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80530/discussion-between-rokibul-hasan-and-yerassyl). – Rokibul Hasan Jun 14 '15 at 19:49
  • {"map"=>[{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}]} – yerassyl Jun 14 '15 at 19:49
  • 1
    Can you please print this `puts map_params["map"]`, if it returns `[{"lat"=>"51.088672", "lon"=>"71.396522", "vibration_level"=>"300", "time_sent"=>"07:25:00"}, {"lat"=>"51.088672", "lon"=>"71.396453", "vibration_level"=>"300", "time_sent"=>"07:25:01"}, {"lat"=>"51.088829", "lon"=>"71.396476", "vibration_level"=>"300", "time_sent"=>"07:25:14"}]` then use `Map.create(map_params["map"])`, let me if works? – Rokibul Hasan Jun 14 '15 at 19:52
  • @Rakibul Hasan, yes it worked. Thank you very much. Edit your answer i will accept it. – yerassyl Jun 14 '15 at 19:54