开发者

Multiple relation with same model

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

and

class User < ActiveRecord::Base
has_many :assets
end

now in assets show.html.erb i can acces开发者_Python百科s name of creator with

 @asset.creator.name

but i can not see name of 'assigned_to'

@asset.assigned_to.name  #gives nothing

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


Try printing the values directly to console:

puts @asset.pretty_inspect
puts @asset.assigned_to.pretty_inspect

Something;s not right :)


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

and

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜