开发者

One-To-Many-Through adding join model instances

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开发者_JS百科 => test, :result => result)
end
@visit.save

where variable @results is Hash (keys: Test, values: String)


It raises error because child objects are not saved. Try this:

@results.each do |test, result|
  TestVisit.create(:visit => @visit, :test => test, :result => result)
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜