Hi there,
I''ve recently started using Rails, and I''m already enthousiast
about it!
I keep facing the same problem: I want to proveut an attribute in my
model from being saved to the database (because it''s a
``date'''' field,
which is default set by my database, which I like) without any luck: my
database complains about getting a NULL value in the ``date''''
field (in
table special_decla), which violates a non-null constraint:
--- snip ---
ActiveRecord::StatementInvalid in Decla#save_decla
ERROR: null value in column "date" violates not-null constraint :
INSERT INTO special_decla ("name", "date",
"amount", "description",
"person_id") VALUES(''test'', NULL, 28.0,
''test'', 1)
--- snip ---
Having read the (well some, not all) docs on rubyonrails.com, I modified
my code to ``attr_procted :data'''', without any luck. I tried
to
``attr_accessible'''' all the elements which _can_ be saved, but
without
luck too. The only way it works, is by not doing
``decla.attributes=@param[''decla'']'''', and
explicitly assigning the
attributes. I thought that the protection of elements of mass
assignment just should work and that this approach should not be needed,
hence this mail :-)
I suspect there is something going wrong here, but I do not know what.
Can anyone point me in the right direction?
I can imagine that it''s perhaps somewhat unusual to
``set_table_name'''',
but the database schema is something beyond my power, and without those
it simply does not work at all (rails is unable to find my tables).
Some details: I''m using Rails 0.13.1 (debian rails-0.13.1-1 package),
my
database is PostgreSQL and I''m using Ruby 1.8.
the code:
class Decla < ActiveRecord::Base
set_table_name "special_decla"
# do not save the date, leave this to the database
attr_protected :date
has_one :person
end
class Person < ActiveRecord::Base
set_table_name "person"
has_many :declas
end
class DeclaController < ApplicationController
def save_decla
item = Decla.new
item.attributes = @params["decla"]
if item.save
redirect_to(:action => "list")
else
render_text "Error"
end
end
end
best regards,
Jorik Jonker