2010-08-16 72 views
2

我計劃使用該模塊(全exmpample這裏http://pastie.org/1098444將公共屬性注入activerecord類的正確方法是什麼?

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 may not be 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/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract_adapter.rb:202:in rescue in log' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract_adapter.rb:194:in log' /Users /janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in execute' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/database_statements.rb:239:in insert_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/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:570:in每個' /Users/janlimpens/.rvm/gems/ [email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:570:在insert_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:in塊(4級)in create_fixtures' /Users/janlimpens/.rvm/gems /[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:in each' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:in block( 3個等級)in create_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract/database_statements.rb:139 :在transaction' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:512:in block(2 levels)in create_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/abstract_adapter。 rb:104:在012_block in create_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activesupport-3.0.0.rc/lib/active_support/benchmarkable.rb:55 :silence' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:502:in create_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:961:in load_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:926:in setup_fixtures' /Users/janlimpens/.rvm/gems/[email protected]/gems/activesupport-3。 0.0.rc/lib/active_support/callbacks.rb:409:在_run_setup_callbacks' /Users/janlimpens/.rvm/gems/[email protected]/gems/activesupport-3.0.0.rc/lib/active_support/testing/setup_and_teardown.rb:34:in運行'

回答

1

我建議添加validates_presence_of :name捕獲紅寶石側的錯誤。您的應用程序會嘗試將看似有效的對象保存到數據庫中,名稱爲NULL,Ruby允許這樣做。現在你的DB爆炸了,因爲你在那裏設置了一個NOT NULL,它不喜歡你在那裏保存的東西。

+0

不應該這樣做嗎? – Jan 2010-08-16 00:09:27

+0

,它確實不會改變任何東西。我不斷收到相同的錯誤。 – Jan 2010-08-16 01:21:00

+0

哦,對不起。 #read_attribute不是你想要的。好像#[] =也可以。我認爲你的問題是你保存對象過早並且在你的數據庫中有一些NOT NULL列。我建議添加'validates_presence_of:name'來捕獲ruby方面的錯誤。 – Reactormonk 2010-08-16 01:46:20

相關問題