0

<<演算子を使用して関係を追加しようとすると問題が発生します。追加後に保存すると、コールバックが実行されないようです。

私は2つのモデルを持っています:

ファブリック

class Fabric
  include DataMapper::Resource

  property :id,           Serial
  property :name,         String
  property :fixed_color,  Boolean
  property :active,       Boolean, :default => true

  has 1,      :cut
  has n,      :colors, :through => Resource

  after :save, :add_to_fishbowl

  def add_to_fishbowl
    puts self.colors.size
  end
end

class Color
  include DataMapper::Resource

  property :id,   Serial
  property :name, String

  has n,      :cut
  has n,      :fabrics, :through => Resource
end

2 つの色と生地を作成します。

yellow = Color.new(:name => "yellow")
red = Color.new(:name => "red")

f = Fabric.create(:name => "tricot", :fixed_color => false)

追加演算子を使用した場合、コールバックは実行されません。

f.colors << red
f.save
f.colors << yellow
f.save
puts f.colors.size
=> 0
=> 2

配列を追加すると、次のようになります。

f.colors = f.colors + [red]
f.save
f.colors = f.colors + [yellow]
f.save
puts f.colors.size
=> 0
=> 1
=> 2
=> 2

ruby 1.9.3p392 と data_mapper (1.2.0) を実行しています。

4

1 に答える 1

0

リレーションがありません。n 個、:cuts があります

class Color
  include DataMapper::Resource

  property :id,   Serial
  property :name, String

  has n,      :cuts
  has n,      :fabrics, :through => Resource
end
于 2013-04-25T20:37:23.713 に答える