In my model I have the following:
class Dyno < ActiveRecord::Base
include FriendlyId
friendly_id :slug_candidates, :use => [:slugged, :history, :finders]
def should_generate_new_friendly_id?
car.present? && horsepower_changed?
end
def slug_candidates
[
[car.owner, :horsepower, "horsepower-dyno"],
[car.owner, car.nickname, :horsepower, "horsepower-dyno"]
]
end
When I try to update a record: Before a slug is generated, I have no problem running my validations and getting all my validation messages, but soon as the slug is generated, and i hit save I get 404. Even if i put in all the proper fields, and hit save which would pass validations it is not saving the record and returning a 404
It's looks like it's trying to look up and save the record by the new "-slug-" before that slug exists in the record.
Started PATCH "/dynos/john-smith-1800-horsepower-dyno" for 127.0.0.1 at 2014-03-09 12:50:03 -0400
Processing by DynosController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rxz8PPyTYgQx6umBB7t50fLtPUk4zGrCmSiwd751jxI=", "dyno"=>{"car_id"=>"1", "horsepower"=>"1800", "torque"=>"555", "boost"=>"", "nitrous"=>"no", "nitrous_amount"=>"", "fuel_id"=>"2", "tuner"=>"", "video_url"=>"", "dyno_brand_id"=>"", "details"=>""}, "commit"=>"Update Dyno", "id"=>"john-smith-1800-horsepower-dyno"}
[1m[35mDyno Load (0.6ms)[0m SELECT "dynos".* FROM "dynos" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_id" = "dynos"."id" AND "friendly_id_slugs"."sluggable_type" = 'Dyno' WHERE ("friendly_id_slugs"."sluggable_type" = 'Dyno' AND "friendly_id_slugs"."slug" = 'john-smith-1800-horsepower-dyno') ORDER BY "dynos"."id" ASC LIMIT 1
Completed 404 Not Found in 5ms
In my controller I am using the .friendly scope
def set_dyno
@dyno = Dyno.friendly.find(params[:id])
end
Also, in my controller I am doing a save in the new action so that i can let users add associated pictures from the same new view. Could maybe Friendly_Id may be reacting to a nil slug in a existing record?
def new @dyno = Dyno.new @dyno.save :validate => false end
What am I doing wrong?
I am using friendly_id 5.0.2