Ruby on rails 获取friendlyId自定义slug的另一个模型中的用户名
我在用FriendlyId。 目前,我以这种方式构建了一个自定义slug:Ruby on rails 获取friendlyId自定义slug的另一个模型中的用户名,ruby-on-rails,ruby-on-rails-4,friendly-id,Ruby On Rails,Ruby On Rails 4,Friendly Id,我在用FriendlyId。 目前,我以这种方式构建了一个自定义slug: 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 class-Photo:sluged def photo_
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
class-Photo:sluged
def photo_作者
“#{title}由#{user_id}生成”
结束
属于:用户
结束
我的鼻涕虫就像:/photo-title-by-7
我想获取user=7的username
而不是7
我如何才能做到这一点?委托
好的老方法怎么样:
#app/models/photo.rb
Class Photo < ActiveRecord::Base
belongs_to :user
delegate :username, to: :user
end
def photo_by_author
"#{title} by #{username}"
end