0

I'm using FriendlyId. At the moment i build a custom slug this way:

class Photo < ActiveRecord::Base
extend FriendlyId
friendly_id :photo_by_author, :use => :slugged

 def photo_by_author
   "#{title} by #{user_id}"
 end

belongs_to :user

end

And my slug it's like: /photo-title-by-7 Instead of 7 I want to get the username of the user = 7

How can I do this?

Igor Martins
  • 2,015
  • 7
  • 36
  • 57

1 Answers1

2

Delegate

How about the good old .delegate method:

#app/models/photo.rb
Class Photo < ActiveRecord::Base
   belongs_to :user
   delegate :username, to: :user
end 

This will allow you to call:

def photo_by_author
   "#{title} by #{username}"
 end
Richard Peck
  • 76,116
  • 9
  • 93
  • 147