2

我计划使用这个模块(完整的例子在这里http://pastie.org/1098444

puts "Name_and_key was referenced."

module Name_and_key

  def normalize(s)
    s.mb_chars.normalize(:kd).gsub(/[^\-x00-\x7F]/n, '').to_s
  end

  def name=(name)
    self[:name] = name
    self[:key] = normalize(name).downcase
  end

  def name
    self[:name]
  end

  def key=(key)
    self[:key] = normalize(key).downcase
  end

  def key
    self[:key]
  end

end

但似乎这些值并没有到达模型。

 class Category < ActiveRecord::Base
  include Name_and_key

  has_many :tiles
  validates_presence_of :name, :key
end

cat = Category.create do |c|
 c.name = "cat"
end

ActiveRecord::StatementInvalid: SQLite3::ConstraintException: categories.name 不能为 NULL: INSERT INTO "categories" ("created_at", "updated_at", "id") VALUES ('2010-08-15 23:20:43' , '2010-08-15 23:20:43', 980190962)

这是否是一种有效的方法,如果不是,怎么可能做到这一点?如果确实如此,我的错误是什么?

失败的单元测试

test "can be created" do
cat = Category.create do |c|
  c.name = "cat"
end
tile = Tile.create do |t|
  t.name = "test"
  t.category = cat
end
assert tile.save

结尾

一些痕迹

  1) Error:

test_can_be_created(TileTest): ActiveRecord::StatementInvalid: SQLite3::ConstraintException: categories.name 不能为 NULL: INSERT INTO "categories" ("created_at", "updated_at", "id") VALUES ('2010-08-16 02 :06:43', '2010-08-16 02:06:43', 980190962) /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc /lib/active_record/connection_adapters/abstract_adapter.rb:202:in rescue in log' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract_adapter.rb:194:inlog' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/ connection_adapters/sqlite_adapter.rb:135:in execute' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/database_statements.rb:239:ininsert_fixture' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:634 :在block in insert_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:570:in每个'/Users/janlimpens/.rvm/gems/ruby-1.9。2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:570:ininsert_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:increate_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:ineach' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:in块中的块(4 级) (3 个级别)在 create_fixtures 的 /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/database_statements.rb:139 :在transaction' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:512:increate_fixtures'/Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract_adapter.rb中的块(2级)中: 104:indisable_referential_integrity' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:503:in块在 create_fixtures'/Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activesupport-3.0.0.rc/lib/active_support/benchmarkable.rb:55 :insilence' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:502:increate_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:961:in load_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:926:insetup_fixtures' /Users/janlimpens/ .rvm/gems/ruby-1.9.2-rc2@rails3/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:409:in _run_setup_callbacks' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activesupport-3.0.0.rc/lib/active_support/testing/setup_and_teardown.rb:34:inrun'

4

1 回答 1

1

我建议添加validates_presence_of :name以在 ruby​​ 方面捕获这些错误。您的应用程序尝试将看似有效的对象保存到数据库,名称为 NULL,这是 ruby​​ 方面允许的。现在你的数据库爆炸了,因为你在那里设置了一个 NOT NULL,它不喜欢你在那里保存的东西。

于 2010-08-16T00:06:38.420 回答