0
我有两个彼此相关的模型(多对多),我想在我的Rails控制器操作的响应中返回它们。使用Datamapper从Rails控制器返回关联对象
这两个类是用户和位置。还有一个链接类UserLocation。
User.rb样子:
class User
include DataMapper::Resource
...
has n, :user_locations
has n, :locations, :through => :user_locations
end
UserLocation.rb:
class UserLocation
include DataMapper::Resource
# attributes
property :id, Serial
# relationships
belongs_to :user
belongs_to :location
# validation
validates_presence_of :user, :location
end
Location.rb:
class Location
include DataMapper::Resource
# attributes
# no need to specify the user relation AFAIK
end
当我做了User.get(id)
它返回所有的用户属性但不是位置。我可以通过代码进行调试并运行user.locations
,它可以正常工作。为什么不从轨道行动返回的位置?
那么如何指定我希望加载的关系? – Ger 2012-04-21 11:58:47
您不会在'#get'上加载关系。你调用'user.locations'并使用它的结果。 – ujifgc 2012-05-01 06:16:02