# ActiveRecord before\_ callbacks question

**URL:** <https://www.ruby-forum.com/t/activerecord-before-callbacks-question/54057>\
**Category:** Rails\
**Created:** [February 21, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/activerecord-before-callbacks-question/54057 "2006-02-21T20:11:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 21, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/activerecord-before-callbacks-question/54057/1 "2006-02-21T20:11:07Z")

</div>

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:in`send’  
/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:in`to\_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

---

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 21, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/activerecord-before-callbacks-question/54057/2 "2006-02-21T20:31:05Z")

</div>

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:in`send’  
> /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:in`to\_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

---

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 21, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/activerecord-before-callbacks-question/54057/3 "2006-02-21T20:54:24Z")

</div>

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
