0

When we use friendly_id, Rails can't update multiple records at a time. Here's my controller line which update the records :

@submitsup = Photo.update(params[:submits].keys, params[:submits].values)

Here's the error I get :

ActiveRecord::RecordNotFound (Couldn't find Photo with 'id'=hello-world)

It have used the slug as the ID. How can I fix this? (Please note that I update multiple records at the same time.)

Generated params (updated with the id):

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"3WERrJpTdyzzpepkjpzMAZFV67Ogw6yWBICOE/s0o6TgGotO9OpFIFygt7q/oEBcVDNLNkP52cr2PSJ5qWwInQ==", "submits"=>{"hello-world"=>{"accepted"=>"1", "rejectreason"=>"", "id"=>"15922356", "category"=>"Technology", "tags"=>"{hello,go,web}", "nsfw"=>"0"}, "hello-world-37380779"=>{"accepted"=>"0", "rejectreason"=>"", "id"=>"37380779", "category"=>"Technology", "tags"=>"{hello,go,web}", "nsfw"=>"0"}}, "commit"=>"Update All", "controller"=>"dash", "action"=>"update_submits"}
THpubs
  • 7,804
  • 16
  • 68
  • 143

1 Answers1

0

Found the answer from the git issue I opened : https://github.com/norman/friendly_id/issues/631

numeric_ids = params[:submits].keys.map do |key|
  if key.friendly_id?
    Photo.friendly.find(key).id
  else
    key
  end
end

@submitsup = Photo.update(numeric_ids, params[:submits].values)
THpubs
  • 7,804
  • 16
  • 68
  • 143