1

I have a model called Row:

# == Schema Information
#
# Table name: rows
#
#  id         :integer          not null, primary key
#  created_at :datetime
#  updated_at :datetime
#  numCols    :integer
#  type       :string(255)
#  content    :text
#  status     :string(255)
#  doc_id     :integer
#

class Row < ActiveRecord::Base
    belongs_to :doc, :foreign_key => "doc_id", :class_name => "Doc"

=begin
Status of a row can be:
1) header
2) body
3) ignore
=end

Some relevant methods from my rows_controller are as follows:

 def update_multiple
    puts "update multiple"
    puts "#{row_params.inspect}"
    #How to use Update ALL
    #Row.update_all(row_params[:rows])
    redirect_to docs_path
  end

def row_params
      #params[:row]
        params.permit(:row, :rows =>[:id, :status, :content])
    end

The row_params sent from a form to the update_multiple method look like this:

   Parameters: {"utf8"=>"✓", "authenticity_token"=>"72nyykQ3uOL+VSicBk99HOtyCDpqDe4FaLzOa6EILYI=", "commit"=>"Update Row Classification", 
"rows"=>{"1"=>{"id"=>"1", "status"=>"ignore", "content"=>"[PROGRAMS] (All Locations|Consolidated),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"}, 

"2"=>{"id"=>"2", "status"=>"header", "content"=>",,,,,,,,Stats >>,Rtg%,,,,,,,,,,,,,,,Shr% [Total TV Eng],,,,,,,,,,,,,,,#Stations,,,,,,,,,,,,,,"}, 

"3"=>{"id"=>"3", "status"=>"header", "content"=>"Areas,Program,Channel,Date,Weekday,Start time,End time,Duration,Episode Name,Ind.2+,A18+,A25-54,A18-49,A18-34,A55+,F25-54,F18-49,F18-34,M25-54,M18-49,M18-34,A12-34,T12-17,C2-11,Ind.2+,A18+,A25-54,A18-49,A18-34,A55+,F25-54,F18-49,F18-34,M25-54,M18-49,M18-34,A12-34,T12-17,C2-11,Ind.2+,A18+,A25-54,A18-49,A18-34,A55+,F25-54,F18-49,F18-34,M25-54,M18-49,M18-34,A12-34,T12-17,C2-11"},

....

I want to update all the rows that are included in the params with the values sent along in the update_multiple method shown above

How do I do this? Im confused

Thanks a lot for your help.

banditKing
  • 9,405
  • 28
  • 100
  • 157

1 Answers1

0

As you can see from docs you need to specify what rows you want to update, and then implement it with the same data (or calculation like number = number + 1, where number is property). But in you case the values is different, so you should find rows and separately set values.

In your case you it should be like:

rows = Row.find(row_params[:rows].map { |r| r[:id] })
rows.each { |r| r.update(row_params[:rows]) }
zishe
  • 10,665
  • 12
  • 64
  • 103