ActiveRecord before_ callbacks question


#1

Hi!

I need to set the primary key in a model and postgres table to something
different than id. View Edit and such works in the scaffolded
controller. However if I click in the controller below the list of data
onto New then I get an error message like this:

Showing app/views/admin/_form.rhtml where line #5 raised:

undefined method `nr_before_type_cast’ for #Adressen:0x40aa6e8c

Extracted source (around line #5):

2:
3:
4:

Nr

5: <%= text_field ‘adressen’, ‘nr’ %>


6:
7:

Anrede

8: <%= text_field ‘adressen’, ‘anrede’ %>

Trace of template inclusion: /app/views/admin/new.rhtml

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in
method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:253:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:253:in
value_before_type_cast' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:168:into_input_field_tag’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:76:in
`text_field’
#{RAILS_ROOT}/app/views/admin/_form.rhtml:5
#{RAILS_ROOT}/app/views/admin/new.rhtml:4

Adressen.rb looks like this:
class Adressen < ActiveRecord::Base
set_table_name “adressen”
set_primary_key “nr”
end

Which before_ callback do I need to implement to set nr to something
programmatically before the view wants to display text_field
‘adressen’,‘nr’ and crashes? I tried to use before_create and set
self.id or self.nr to some string but had no success, same error like
above.

Thank you,
Philipp


#2

Philipp Ott wrote:

Hi!

I need to set the primary key in a model and postgres table to something
different than id. View Edit and such works in the scaffolded
controller. However if I click in the controller below the list of data
onto New then I get an error message like this:

Showing app/views/admin/_form.rhtml where line #5 raised:

undefined method `nr_before_type_cast’ for #Adressen:0x40aa6e8c

Extracted source (around line #5):

2:
3:
4:

Nr

5: <%= text_field ‘adressen’, ‘nr’ %>


6:
7:

Anrede

8: <%= text_field ‘adressen’, ‘anrede’ %>

Trace of template inclusion: /app/views/admin/new.rhtml

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in
method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:253:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:253:in
value_before_type_cast' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:168:into_input_field_tag’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_view/helpers/form_helper.rb:76:in
`text_field’
#{RAILS_ROOT}/app/views/admin/_form.rhtml:5
#{RAILS_ROOT}/app/views/admin/new.rhtml:4

Adressen.rb looks like this:
class Adressen < ActiveRecord::Base
set_table_name “adressen”
set_primary_key “nr”
end

Which before_ callback do I need to implement to set nr to something
programmatically before the view wants to display text_field
‘adressen’,‘nr’ and crashes? I tried to use before_create and set
self.id or self.nr to some string but had no success, same error like
above.

Thank you,
Philipp

You shouldn’t be allowed to edit the primary key, but if you really want
to:
change

4:

Nr

5: <%= text_field ‘adressen’, ‘nr’ %>


to

Nr
<%= text_field 'adressen', 'id' %>

Even thought youve specified a different id, you still acces it throught
id.

Joey


#3

Hi!

joey__ wrote:

<%= text_field ‘adressen’, ‘id’ %>

Even thought youve specified a different id, you still acces it
throught id.

Thank you, yes I remember I have to think write and code “id” everywhere
although it is nr. This implies checking scaffolded sources too. OK. It
is working now. However my questions remains, how upon .new do i create
a new id/nr by myself? I used before_create and id/nr stays empty when
it shows up in the form view upon New. Or do I need after_create?

Regards,
Philipp