[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

Am Mittwoch, den 15.03.2006, 11:44 +0100 schrieb Raphael S.:

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 T.

http://blog.inlet-media.de

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 :slight_smile:

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]
}
)
[…]
——

—<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
[…]
——

—<snip: views/application/_item.mab>—
[…]
render :partial => tpl, :object => item
[…]
——

—<example snip: views/contacts/_contact.mab>—
[…]
contact.address # <-- This works…
[…]
contact.account.name # <-- Here’s the problem!
[…]
——

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
——

That’s quite a lot of code now, hopefully it’ll help to explain the
problem…

  • Raphael
  1. Associate an empty Account when Contact is created

model !completely untested!

class Contact < ActiveRecord::Base
after_create :create_account
end

  1. 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 :slight_smile:

Thanks a million Norman!

– Raphael

Am Mittwoch, den 15.03.2006, 15:02 +0100 schrieb Raphael S.:

“New #{@label}”,
—<snip: controllers/application.rb>—
end
end
[…]
belongs_to :account
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :account_id
end
——

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

  1. 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 T.

http://blog.inlet-media.de