与相同型号的倍数关系 [英] Multiple relation with same model

查看:172
本文介绍了与相同型号的倍数关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模式资产和用户.'Asset'可以被分配给一个'用户'和'资产'是由一个创建user'.here是模型类的细节

i have two model 'Asset' , and 'User' .'Asset' can be assigned to one 'user' and 'asset' is created by one 'user'.here is detail of model classes

class Asset < ActiveRecord::Base
belongs_to :assigned_to ,:class_name=>'User'
belongs_to :creator ,:class_name=>'User'
end

class User < ActiveRecord::Base
has_many :assets
end

现在的资产show.html.erb我可以用

now in assets show.html.erb i can access name of creator with

 @asset.creator.name

但我看不到'assigned_to

but i can not see name of 'assigned_to'

@asset.assigned_to.name  #gives nothing

这两个值成功保存在database.what是什么问题?

both values are successfully saved in database.what is problem?

推荐答案

最后,我的问题是解决了这里是解决方案

finally my problem is solved here is solution

class Asset < ActiveRecord::Base

belongs_to :creator ,:class_name=>'User'
belongs_to :assigned_to, :class_name=>'User' 

end

user.rb

class User < ActiveRecord::Base

has_many :created_assets, :foreign_key => 'creator_id', :class_name => 'Asset'
has_many :assigned_assets , :foreign_key => 'assigned_to_id', :class_name => 'Asset'

end

这篇关于与相同型号的倍数关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆