I am using the gems Workflow, Paper Trail and Friend ID.
To track the state changes using Paper Trail, I have overridden the persist_workflow_state to explicitly update the workflow column, so that Paper Trail can capture the change.
https://github.com/geekq/workflow#integration-with-activerecord
def persist_workflow_state(new_value)
update_attribute self.class.workflow_column, new_value
end
Now, I have introduced Friendly ID without slug column, and I get the error, upon reaching the above method.
undefined method `slug=' for #<ModelName:0x007f81cf342cd8>
Any help?
Basic point is that, if you're using the friendly ID gem, then you will need to add a slug column, to the relevant ActiveModel table.
Each Project has a name (attribute).
class Project < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, :state, presence: true
end
This can be done easily in rails:
rails g migration AddSlugToProjects slug
Make sure you add in an index on the slug column:
class AddSlugToProjects < ActiveRecord::Migration[5.2]
def change
add_column :projects, :slug, :string
add_index :projects, :slug
end
end
And now you're off to the races!
Now, I have introduced Friendly ID without slug column
I don't know exactly what you mean here, but simply, it's like trying to start a car without the key
FriendlyID
The way FriendlyID works is to use a slug
(or other identifier) column to both create the slugged URL, and find based off the slugged ID:
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
This allows the gem to find based off either the id
or the slug
attribute
If you miss out the slug
column, this will prevent this from working, causing your error. The ways to fix this are:
attr_accessor
If you'd like to try the second option, you could try this:
#app/models/ModelName.rb
attr_accessor :slug
Richard Peck is correct that you have to add slug
field.
But lot's of people got confused because FriendlyID generates friendly_id_slugs
table which contains sluggable_id and sluggable_type field.
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
t.index ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
t.index ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
end
Basically it generates friendly_id_slugs table for History Module. Have a look at their documentation about History__Avoiding_404_s_When_Slugs_Change : http://norman.github.io/friendly_id/file.Guide.html#History__Avoiding_404_s_When_Slugs_Change
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With