Forum: Ruby on Rails ActiveRecord before_ callbacks question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E5394e4d020f5007cfbda9c2522aff63?d=identicon&s=25 Philipp Ott (Guest)
on 2006-02-21 20:11
(Received via mailing list)
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: <!--[form:adressen]-->
4: <p><label for="adressen_nr">Nr</label><br/>
5: <%= text_field 'adressen', 'nr'  %></p>
6:
7: <p><label for="adressen_anrede">Anrede</label><br/>
8: <%= text_field 'adressen', 'anrede'  %></p>

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
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-02-21 20:31
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: <!--[form:adressen]-->
> 4: <p><label for="adressen_nr">Nr</label><br/>
> 5: <%= text_field 'adressen', 'nr'  %></p>
> 6:
> 7: <p><label for="adressen_anrede">Anrede</label><br/>
> 8: <%= text_field 'adressen', 'anrede'  %></p>
>
> 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: <p><label for="adressen_nr">Nr</label><br/>
> 5: <%= text_field 'adressen', 'nr'  %></p>
to
<p><label for="adressen_nr">Nr</label><br/>
<%= text_field 'adressen', 'id'  %></p>

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

Joey
E5394e4d020f5007cfbda9c2522aff63?d=identicon&s=25 Philipp Ott (Guest)
on 2006-02-21 20:54
(Received via mailing list)
Hi!

joey__ wrote:
> <%= text_field 'adressen', 'id'  %></p>
>
> 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
This topic is locked and can not be replied to.