Forum: Ruby on Rails [NQ] How to set an initial FK for a new item of something?

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.
Raphael S. (Guest)
on 2006-03-15 12:44
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
Norman T. (Guest)
on 2006-03-15 15:34
(Received via mailing list)
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
Raphael S. (Guest)
on 2006-03-15 16:02
> 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
Norman T. (Guest)
on 2006-03-15 16:19
(Received via mailing list)
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
> ---<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 T.

http://blog.inlet-media.de
Raphael S. (Guest)
on 2006-03-15 16:38
> 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
This topic is locked and can not be replied to.