Raphael Schmid
2006-Mar-15 10:45 UTC
[Rails] [NQ] How to set an initial FK for a new item of something?
Hi all, running into a little problem here. I''ve a list of things with a "New thing" link beneath it. When you click that link, obviously, a new thing shall be created. Now, that works pretty fine for things that don''t have foreign keys, but for things that do it crashes, since there is not yet a thing.other_thing.name available. I suspect you can somewhere in app/models/thing.rb tell it to always assign, say, the first other_thing to a new thing... ? Grateful for any hints you might have! Best regards, Raphael -- Posted via http://www.ruby-forum.com/.
Norman Timmler
2006-Mar-15 13:31 UTC
[Rails] [NQ] How to set an initial FK for a new item of something?
Am Mittwoch, den 15.03.2006, 11:44 +0100 schrieb Raphael Schmid:> Hi all, > > running into a little problem here. I''ve a list of things > with a "New thing" link beneath it. When you click that link, > obviously, a new thing shall be created. Now, that works pretty > fine for things that don''t have foreign keys, but for things that > do it crashes, since there is not yet a thing.other_thing.name > available. > > I suspect you can somewhere in app/models/thing.rb tell it to > always assign, say, the first other_thing to a new thing... ? > > Grateful for any hints you might have!My head is so full of things, that it might help if can provide a code example. That would help a lot to answer your question. -- Norman Timmler http://blog.inlet-media.de
Raphael Schmid
2006-Mar-15 14:02 UTC
[Rails] Re: [NQ] How to set an initial FK for a new item of somethin
> My head is so full of things, that it might help if can provide a code > example. That would help a lot to answer your question.Sure :-) The flow is as follows: ---<snip: views/application/list.mab>--- [...] link_to_remote( "New #{@label}", :url => { :controller => ''application'', :action => ''show_item'', :id => ''new'', :class => @helpers.params[:controller] } ) [...] ---<snap>--- ---<snip: controllers/application.rb>--- [...] def show_item controller = params[:class] action = controller.singularize klass = action.capitalize if params[:id] == ''new'' item = Object.const_get(klass).new else item = Object.const_get(klass).find params[:id] end render :update do |p| p.replace_html ''item'', render( :partial => ''application/item'', :object => item, :locals => { :tpl => "#{controller}/#{action}" } ) end end [...] ---<snap>--- ---<snip: views/application/_item.mab>--- [...] render :partial => tpl, :object => item [...] ---<snap>--- ---<example snip: views/contacts/_contact.mab>--- [...] contact.address # <-- This works... [...] contact.account.name # <-- Here''s the problem! [...] ---<snap>--- The model looks like this (Account, of course, has_one :contact): ---<example snip: models/contact.mab>--- class Contact < ActiveRecord::Base belongs_to :account validates_presence_of :first_name validates_presence_of :last_name validates_presence_of :account_id end ---<snap>--- That''s quite a lot of code now, hopefully it''ll help to explain the problem... - Raphael -- Posted via http://www.ruby-forum.com/.
Norman Timmler
2006-Mar-15 14:19 UTC
[Rails] Re: [NQ] How to set an initial FK for a new item of somethin
Am Mittwoch, den 15.03.2006, 15:02 +0100 schrieb Raphael Schmid:> > My head is so full of things, that it might help if can provide a code > > example. That would help a lot to answer your question. > > Sure :-) > > The flow is as follows: > > ---<snip: views/application/list.mab>--- > [...] > link_to_remote( > "New #{@label}", > :url => { > :controller => ''application'', > :action => ''show_item'', > :id => ''new'', > :class => @helpers.params[:controller] > } > ) > [...] > ---<snap>--- > > ---<snip: controllers/application.rb>--- > [...] > def show_item > controller = params[:class] > action = controller.singularize > klass = action.capitalize > > if params[:id] == ''new'' > item = Object.const_get(klass).new > else > item = Object.const_get(klass).find params[:id] > end > > render :update do |p| > p.replace_html ''item'', render( > :partial => ''application/item'', > :object => item, > :locals => { > :tpl => "#{controller}/#{action}" > } > ) > end > end > [...] > ---<snap>--- > > ---<snip: views/application/_item.mab>--- > [...] > render :partial => tpl, :object => item > [...] > ---<snap>--- > > ---<example snip: views/contacts/_contact.mab>--- > [...] > contact.address # <-- This works... > [...] > contact.account.name # <-- Here''s the problem! > [...] > ---<snap>--- > > The model looks like this (Account, of course, has_one :contact): > > ---<example snip: models/contact.mab>--- > class Contact < ActiveRecord::Base > belongs_to :account > validates_presence_of :first_name > validates_presence_of :last_name > validates_presence_of :account_id > end > ---<snap>--- > > That''s quite a lot of code now, hopefully it''ll help to explain the > problem...Ok, i see. Your problem is simple to describe: You want to access attributes of an associated model, but you do not know, if an association has already been set. You have two options: 1. Associate an empty Account when Contact is created # model !completely untested! class Contact < ActiveRecord::Base after_create :create_account end 2. Test if an model has been associated, before accessing its attributes # view <%= contact.account && contact.account.name %> This will only call the name method, if contact.account is not nil. Hope That helps. -- Norman Timmler http://blog.inlet-media.de
Raphael Schmid
2006-Mar-15 14:38 UTC
[Rails] Re: Re: [NQ] How to set an initial FK for a new item of some
> 1. Associate an empty Account when Contact is created > > # model !completely untested! > class Contact < ActiveRecord::Base > after_create :create_account > end > > 2. Test if an model has been associated, before accessing its attributes > > # view > <%= contact.account && contact.account.name %> > > This will only call the name method, if contact.account is not nil.Okay. That''ll help with making it not crash :-) Thanks a million Norman! -- Raphael -- Posted via http://www.ruby-forum.com/.