首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存追加后未运行的回调

保存追加后未运行的回调
EN

Stack Overflow用户
提问于 2013-04-26 03:11:26
回答 1查看 50关注 0票数 0

我在尝试使用<<运算符追加关系时遇到问题。如果我在追加之后保存,我的回调似乎不会运行。

我有两个模型:

交换矩阵

代码语言:javascript
复制
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

颜色

代码语言:javascript
复制
class Color
  include DataMapper::Resource

  property :id,   Serial
  property :name, String

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

我创建了两种颜色和一种织物:

代码语言:javascript
复制
yellow = Color.new(:name => "yellow")
red = Color.new(:name => "red")

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

如果我使用了append操作符,我的回调就不会运行:

代码语言:javascript
复制
f.colors << red
f.save
f.colors << yellow
f.save
puts f.colors.size
=> 0
=> 2

如果我添加数组,它是:

代码语言:javascript
复制
f.colors = f.colors + [red]
f.save
f.colors = f.colors + [yellow]
f.save
puts f.colors.size
=> 0
=> 1
=> 2
=> 2

我运行的是Ruby1.9.3p392和data_mapper (1.2.0)。

EN

回答 1

Stack Overflow用户

发布于 2013-04-26 04:37:24

你缺少关系,切分有n,:

代码语言:javascript
复制
class Color
  include DataMapper::Resource

  property :id,   Serial
  property :name, String

  has n,      :cuts
  has n,      :fabrics, :through => Resource
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16222862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档