# Newb question:

**URL:** <https://www.ruby-forum.com/t/newb-question/110205>\
**Category:** Rails\
**Created:** [July 24, 2007, 6:49am UTC](https://www.ruby-forum.com/t/newb-question/110205 "2007-07-24T06:49:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 24, 2007, 6:49am UTC](https://www.ruby-forum.com/t/newb-question/110205/1 "2007-07-24T06:49:39Z")

</div>

Hi all,

Another newb online… trying belongs\_to and has\_one constructs for  
the first time.

table Location defined as:  
nickname, street, city, … , contact\_id

the model includes “belongs\_to :contact”

table Contact defined as:  
name, title, … , location\_id

the model includes “has\_one :location”

When I build a list\_contacts view, I assume  
h(contact.location.nickname) will make the connection. However Rails  
returns nil for contact.location. (Verified the linkage is present,  
thanks to script/console.) Seems like I’m missing something very  
simple, but I’m stumped. Ideas?

Best,  
Brian  
Austin, Texas

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 24, 2007, 7:02am UTC](https://www.ruby-forum.com/t/newb-question/110205/2 "2007-07-24T07:02:29Z")

</div>

removed\_email\_address@domain.invalid wrote:

> When I build a list\_contacts view, I assume  
> h(contact.location.nickname) will make the connection. However Rails  
> returns nil for contact.location. (Verified the linkage is present,  
> thanks to script/console.) Seems like I’m missing something very  
> simple, but I’m stumped. Ideas?
> 
> Best,  
> Brian  
> Austin, Texas

Can you post your controller code for list\_contacts? If you have  
‘dereferenced’ your scaffold code also make sure you have an instance  
variable that holds the information for your contact in the controller  
or else your view wont have anything to display.

Also, maybe you just left this out, but contact.location is nil but is  
just contact nil also?

Lastly, your table should be named w/ pluralizes names.

Ex: ‘create table Contacts …’ and ‘create table Locations…’ not the  
singular version you posted in your original mail. Remember models are  
singular and the db tables they are tied to are pluralized.

I’ll assume that you posted just pseudo code but I had to mention it  
just in case.

Hope this, at the least, kicks off a direction for a solution to your  
problem.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 24, 2007, 7:17am UTC](https://www.ruby-forum.com/t/newb-question/110205/3 "2007-07-24T07:17:14Z")

</div>

Hello Jean,  
I appreciate the reply. As requested:

# controller:

def list\_contacts  
@contact\_pages = Paginator.new self, Contact.count, 60, params[:page]  
@contacts = Contact.find :all, :order =\> params[‘order’] ||  
‘lastname’,  
:limit =\> @contact\_pages.items\_per\_page,  
:offset =\> @contact\_pages.current.offset  
end

# view:

… various html …  
\<% for contact in @contacts  
# location = Location.find(contact.location\_id)  
%\>  
  
\<%= link\_to h(contact.lastname+‘,’+contact.firstname), :action  
=\> ‘edit\_contact’, :id =\> contact.id %\>  
\<%= h(contact.title) %\>  
\<%= h(contact.email1) %\>  
\<%= h(contact.phone1) %\>  
\<%= h(contact.location.nickname) %\>  
… various html …  
\<% end %\>

# Notice the "Location.find(contact.location\_id) … this works…

Tks again…  
Brian

On Jul 24, 12:02 am, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 24, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/newb-question/110205/4 "2007-07-24T17:55:00Z")

</div>

Chris wrote:

> Option 1:  
> contacts = Contact.find(:all)
> 
> # Each call to c.location triggers a DB query to delay load the
> 
> location for that contact  
> contacts.each { |c| c.location.nickname }
> 
> Option 2:  
> contacts = Contact.find(:all, :includes =\> [:location])
> 
> # All locations are loaded upfront, so there is only ever 1 DB call
> 
> contacts.each { |c| c.location.nickname }
> 
> Cheers,  
> Chris
> 
> On Jul 24, 3:02 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Adding to this, remove any DB calls in your view. this breaks the model  
/ view/ controller architecture as you’re having a view interact  
directly WITH the Database.

Encapsulate ALL ‘business logic / use cases’ and database access in your  
controller and let your view determine how all the data ‘sent’ to it is  
displayed. Even using a loop and querying the db for your location,  
although a minor infraction, is considered logic and not display.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 24, 2007, 9:27am UTC](https://www.ruby-forum.com/t/newb-question/110205/5 "2007-07-24T09:27:20Z")

</div>

You have 2 columns locations.contact\_id and contacts.location\_id. Of  
these, only locations.contact\_id is used by the associations you have  
defined.

class Location \< AR::Base

# This will lookup locations.contact\_id to find the association

belongs\_to :contact  
end

class Contact \< AR::Base

# This will also lookup locations.contact\_id to find the association

has\_one :location  
end

Make sure that locations.contact\_id is populated in your DB, and that  
you haven’t mistakenly populated contacts.location\_id instead.

BTW, if you need to query a list of contacts, think about the  
performance of using associations.

Option 1:  
contacts = Contact.find(:all)

# Each call to c.location triggers a DB query to delay load the

location for that contact  
contacts.each { |c| c.location.nickname }

Option 2:  
contacts = Contact.find(:all, :includes =\> [:location])

# All locations are loaded upfront, so there is only ever 1 DB call

contacts.each { |c| c.location.nickname }

Cheers,  
Chris

On Jul 24, 3:02 pm, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
