3

I have a model User which is polymorphically associated with Person and Company model.

I am using friendly_id to show permalinks for both Person and Company.

Person has first_name attribute and I put the following in my model.

extend FriendlyId
friendly_id :first_name

Now when I update my existing entries then the slug gets created perfectly. But issue arises when I try creating a new use which throws a routing error.

Secondly, every person has one user and user has a username, so when I try this following command, then the update and new record both throws unknown method username for nil class error.

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

def person_permalink
  "#{self.user.username} #{self.name}"
end
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
Kumar Navneet
  • 81
  • 2
  • 5
  • _But issue arises when I try creating a new use which throws a routing error_ please provide more information on this. You're saying there's an error but not providing any stack traces – Lee Jarvis Mar 15 '12 at 10:57

1 Answers1

2

I believe this may have to do with the before_save on the friendly_id.

more than likely the associated records and ids are not in sync at the time the friendly_id is being created and self.user is nil.

There is a similar issue with people attempting to use the primary key, ID table-column-value within the friendly_id.

Matenia Rossides
  • 1,406
  • 9
  • 35