0

Im following the casts http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?autoplay=true on friendly_id

Having a user model with field "login" And a profile model with a "user_id" that belongs to user

How could I setup friendly_id so that it uses the login field from User model for its slug and get /profiles/1 to /profiles/username ( = login from User model )

Rubytastic
  • 15,001
  • 18
  • 87
  • 175

1 Answers1

1

Friendly_id will use the column or method name you provide in friendly_id configuration. So, you can do this:

class Profile
  belongs_to :user

  friendly_id :profile_permalink, use: :slugged

  protected

  def profile_permalink
    user.login
  end

end
fkreusch
  • 1,329
  • 11
  • 14
  • Installed all following instructions and in console User.find_each(:save) doesn't create the slugs. The def profile_permalink suggestions does not work either, I look into it later thx – Rubytastic Jan 24 '12 at 23:41
  • Worked in the end was some strange issue with an outdated gem not sure wich one upgraded gems and restarted app made it work. – Rubytastic Feb 06 '12 at 07:08
  • @fkreush the above method giving me error Couldn't find Profile with id=Usernam – Adt Jan 16 '15 at 16:36