Friendly_id thowing error when creating a new user

I have a User model that is polymorphically linked to the Person and Company model.

I am using friendly_id to display permalinks for Person and Company.

The person has a first_name attribute and I add the following to my model.

extend FriendlyId
friendly_id :first_name


Now when I update existing entries the pool is created fine. But the problem comes up when I try to create a new use that throws a routing error.

Secondly, each person has one user and the user has a username, so when I try to execute this next command, the update and new entry also causes the unknown method username for a nil class error.

extend FriendlyId
friendly_id :person_permalink, use: [:slugged, :history]

def person_permalink
  "#{self.user.username} #{}"



source to share

1 answer

I believe it might be related to before_save

on friendly_id.

it is more than likely that related records and ids are not synchronized at creation time friendly_id, but self.user


There is a similar problem with people trying to use the primary key, the table-column-value id inside friendly_id.



All Articles