Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获取friendlyId自定义slug的另一个模型中的用户名_Ruby On Rails_Ruby On Rails 4_Friendly Id - Fatal编程技术网

Ruby on rails 获取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_

我在用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_作者
“#{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