一对多通过添加连接模型实例
我的模型:
class Test
include DataMapper::Resource
property :id, Serial
property :name, String, :default => ''
has n, :test_visits
has n, :visits, :through => :test_visits
# ...
end
class Visit
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :test_visits
has n, :tests, :through => :test_visits
# ...
end
class TestVisit
include DataMapper::Resource
property :result, String
belongs_to :test, :key => true
belongs_to :visit, :key => true
end
为什么此代码会引发 SaveFailureError?:
@visit.test_visits.clear
@results.each do |test, result|
@visit.test_visits.new(:test => test, :result => result)
end
@visit.save
其中变量 @results 是哈希(键:测试,值:字符串)
My models:
class Test
include DataMapper::Resource
property :id, Serial
property :name, String, :default => ''
has n, :test_visits
has n, :visits, :through => :test_visits
# ...
end
class Visit
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :test_visits
has n, :tests, :through => :test_visits
# ...
end
class TestVisit
include DataMapper::Resource
property :result, String
belongs_to :test, :key => true
belongs_to :visit, :key => true
end
Why this code raises an SaveFailureError?:
@visit.test_visits.clear
@results.each do |test, result|
@visit.test_visits.new(:test => test, :result => result)
end
@visit.save
where variable @results is Hash (keys: Test, values: String)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会引发错误,因为子对象未保存。试试这个:
It raises error because child objects are not saved. Try this: