# \[NQ\] How to set an initial FK for a new item of something?

**URL:** <https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937>\
**Category:** Rails\
**Created:** [March 15, 2006, 11:44am UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937 "2006-03-15T11:44:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 15, 2006, 11:44am UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937/1 "2006-03-15T11:44:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 15, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937/2 "2006-03-15T14:34:25Z")

</div>

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](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 15, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937/3 "2006-03-15T15:02:13Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 15, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937/4 "2006-03-15T15:38:54Z")

</div>

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

Thanks a million Norman!

– Raphael

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 15, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/nq-how-to-set-an-initial-fk-for-a-new-item-of-something/54937/5 "2006-03-15T15:19:55Z")

</div>

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](http://blog.inlet-media.de)
