0

I'm using the friendly_id gem to handle slugs in my Rails 4 app. For simplicity's sake, let's say I have two resources, Missions and Tasks. Tasks are nested inside Missions. Both models are using FriendlyId, and the slugs are working fine. Because a Task belongs to a Mission, a Task has a mission_id on it.

I'm running into a problem when I update a Task. Because the mission_id is coming through the parameters as a slug (a friendly_id string and not an integer id), the mission_id on the Task is being updated as 0, because it's expecting an integer and is being passed a string.

Anyone have any ideas on how to address this?

routes.rb

resources :missions do
  resources :tasks
end

Mission.rb

class Mission < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
  has_many :tasks
end

Task.rb

class Task < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
  belongs_to :mission
end
Tyler
  • 829
  • 11
  • 26

1 Answers1

1

Include finders:

friendly_id :name, use: [:slugged, :finders]

From doc:

Finders are no longer overridden by default. If you want to do friendly finds,
you must do Model.friendly.find rather than Model.find. You can however 
restore FriendlyId 4-style finders by using the :finders addon

This is for version 5.

sites
  • 21,417
  • 17
  • 87
  • 146