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
4

1 回答 1

2

我相信这可能与before_savefriendly_id 上的有关。

在创建friendly_id 时,关联的记录和ID 很可能不同步并且self.user为零。

尝试在friendly_id 中使用主键ID table-column-value 的人存在类似的问题。

于 2012-03-30T00:25:05.410 回答