views:

36

answers:

1

I planned on using this module (full exmpample here 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

but it seems these values do not arrive at the model.

 class Category < ActiveRecord::Base
  include Name_and_key

  has_many :tiles
  validates_presence_of :name, :key
end

and

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)

Is this a valid approach at all, if not how could this be done? If indeed, what is my mistake?

A failing unit test

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

end

Some trace

  1) Error:

test_can_be_created(TileTest): ActiveRecord::StatementInvalid: SQLite3::ConstraintException: categories.name may not be 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:in 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:ineach' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:570:in insert_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:inblock (4 levels) in create_fixtures' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:in each' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:514:inblock (3 levels) in 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:in transaction' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:512:inblock (2 levels) in create_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:104:in disable_referential_integrity' /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activerecord-3.0.0.rc/lib/active_record/fixtures.rb:503:inblock 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:in silence' /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'

A: 

I suggest adding validates_presence_of :name to capture those errors on the ruby side. Your app tries to save a seemingly valid object to the DB, with name being NULL, which the ruby side allows. Now your DB blows up because you set a NOT NULL there, which doesn't like what you're saving there.

Tass
shouldn't this achieve the same?
Jan Limpens
and it really does not change anything. I keep getting the same error.
Jan Limpens
Oh sorry. #read_attribute wasn't what you wanted. Seems like #[]= works too. I think your problem is that you're saving the object premature and got some NOT NULL columns on your DB. I suggest adding `validates_presence_of :name` to capture those errors on the ruby side.
Tass
category validates this.of course the problem are null values. my question is - given the above scenario: why are they null?
Jan Limpens
May I have a full backtrace?
Tass
I put into the question above. Is this the info you required?
Jan Limpens
aha!ruby-1.9.2-rc2 > c = Category.new(:name =>"Test")Name_and_key was referenced.NoMethodError: private method `write_attribute' called for #<Category:0x000001029f3e60> from /Users/janlimpens/.rvm/gems/ruby-1.9.2-rc2@rails3/gems/activemodel-3.0.0.rc/lib/active_model/attribute_methods.rb:364:in `method_missing'
Jan Limpens
I put self[:key] = ... back in and I now can new up items in the console, but I cannot 'create' them.
Jan Limpens
See my answer. Your row (Category instance) is valid ruby-side but not DB-side. That's the only thing I can tell you by what you gave me here. And probably put the backtrace to a pastebin, it looks aweful here.
Tass
in the console, some of the failing tests seem to work just fine...
Jan Limpens
I think we're down to 'Show me a full test that fails. Least code possible plz. Incl. a DB migration that reproduces your schema.'
Tass
Please have a look at http://pastie.org/1098444 (sorry for the delay)
Jan Limpens
I should note this is ruby 1.9.2 and rails 3.
Jan Limpens