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… ?
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:
Associate an empty Account when Contact is created
model !completely untested!
class Contact < ActiveRecord::Base
after_create :create_account
end
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.