# Has\_one not working as expected?

**URL:** <https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903>\
**Category:** Rails\
**Created:** [January 22, 2006, 4:43am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903 "2006-01-22T04:43:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 22, 2006, 4:43am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/1 "2006-01-22T04:43:21Z")

</div>

I messed around with Rails for a while last year, but then got busy and  
had to drop it. However, I now have time again, so I’m starting to play  
around with a simple database schema to model my extensive collection of  
photographic material, including equipment, negatives, photographs,  
etc. I’m running into an issue with using belongs\_to/has\_one methods.  
Read on for an explanation…

I have two postgres database tables, defined as follows:

photo\_dev=\> \d manufacturers  
Table “public.manufacturers”  
Column | Type |  
Modifiers  
--------±-----------------------±--------------------------------------------------------------  
id | integer | not null default  
nextval(‘public.manufacturers\_id\_seq’::text)  
name | character varying(255) | not null  
Indexes:  
“manufacturers\_pkey” primary key, btree (id)

photo\_dev=\> \d filters  
Table “public.filters”  
Column | Type |  
Modifiers  
-----------------±-----------------------±--------------------------------------------------------  
id | integer | not null default  
nextval(‘public.filters\_id\_seq’::text)  
name | character varying(255) | not null  
filter\_factor | numeric(2,1) | not null  
manufacturer\_id | integer | not null  
description | character varying(50) | not null  
Indexes:  
“filters\_pkey” primary key, btree (id)  
Foreign-key constraints:  
“$1” FOREIGN KEY (manufacturer\_id) REFERENCES manufacturers(id) ON  
DELETE CASCADE

The tables contain this test data:

photo\_dev=\> select \* from filters;  
id | name | filter\_factor | manufacturer\_id | description  
----±-----------±--------------±----------------±------------  
24 | Lee Filter | 3.0 | 5 | Deep Red  
(2 rows)

photo\_dev=\> select \* from manufacturers;  
id | name  
----±-----  
5 | Lee  
(1 row)

I’ve cobbled together this test file:  
#!/usr/bin/ruby

require ‘pp’  
require ‘rubygems’  
require\_gem ‘activerecord’

ActiveRecord::Base::establish\_connection( :adapter =\> ‘postgresql’,  
:host =\> ‘localhost’,  
:database =\> ‘photo\_dev’,  
:username =\> ‘photo’,  
:password =\> ‘photo’ )

class Manufacturer \< ActiveRecord::Base  
belongs\_to :filter  
end

class Filter \< ActiveRecord::Base  
has\_one :manufacturer, :foreign\_key =\> ‘id’  
end

filter = Filter.find( 24 );  
pp filter  
print filter.manufacturer, “\n”

man = Manufacturer.find( filter.manufacturer\_id );  
pp man

The first problem I had was that placing this statement:

```
has_one :manufacturer

```

in the Filter class could cause invalid SQL to be generated when trying  
to lookup the manufacturer. For example, I would have seen this SQL in  
the stack trace:

```
SELECT * FROM manufacturers WHERE (manufacturers.filter_id = 24)

```

LIMIT 1

the wrong column name (manufacturers.filter\_id) was used - I had to add  
the :foreign\_key =\> ‘id’ to fix the column name problem. Is this  
normal, or have I defined the tables incorrectly? It appears that the  
foreign key name assumed to also be the name of the primary key in the  
child table, which breaks the assumption that all tables have a primary  
key called ‘id’…

If I run the above script, I get the following output:

#\<Filter:0x40559238  
@attributes=  
{“name”=\>“Lee Filter”,  
“manufacturer\_id”=\>“5”,  
“filter\_factor”=\>“3.0”,  
“id”=\>“24”,  
“description”=\>“Deep Red”}\>  
nil  
#\<Manufacturer:0x40554698 @attributes={“name”=\>“Lee”, “id”=\>“5”}\>

Calling filter.manufacturer is not finding the child record, yet if I  
directly fetch the record, I can find it. What gives?

My guess is that the two problems are related, and something is borked  
either in my code, or in my database definitions.

Anyone have any suggestions?

I’m running on Rails 1.0…

Cheers!

```
-klm.
```

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 22, 2006, 5:25am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/2 "2006-01-22T05:25:34Z")

</div>

I’m using the supplied scaffold.css file and in my layout I have a  
transitional doctype:

Using this doctype and scaffold.css, when error\_messages\_for() is used,  
the  
styles don’t get applied. I believe it is due to the css using  
#ErrorExplanaion and the actual id/class names are errorExplanation.  
Note  
the capitalization of the ‘E’ in error. Fixing the css allowed the  
styles to  
be applied to the default output of error\_messages\_for.

Should this be fixed?

Thanks.

Bob S.

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 22, 2006, 8:14am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/3 "2006-01-22T08:14:51Z")

</div>

Wow!

My scaffolding derived forms look a _lot_ better now.

Thanks!

I think you need to file a bug report for this…

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 22, 2006, 8:23am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/4 "2006-01-22T08:23:59Z")

</div>

Bug report submitted.

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 22, 2006, 6:56am UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/5 "2006-01-22T06:56:28Z")

</div>

On Sat, Jan 21, 2006 at 08:39:59PM -0700, Ken Miller wrote:

> Calling filter.manufacturer is not finding the child record, yet if I  
> directly fetch the record, I can find it. What gives?

Your associations are reversed.

Instead of:

```
Filter
  has_one :manufacturer, ...

Manufacturer
  belongs_to :filter

```

Use:

```
Filter
  belongs_to :manufacturer

Manufacturer
  has_one :filter (or, has_many :filters)

```

The belongs\_to association (whether in a one-to-one or one-to-many  
relationship) always applies to the class whose table has the foreign  
key referencing the other. In your case, your filters table has a FK  
referencing the manufacturer. Ergo, Filter belongs\_to Manufacturer. See  
the API docs[1] for more examples.

Erik.

[1]  
[http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html](http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 27, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/6 "2006-01-27T13:43:40Z")

</div>

baz Scott wrote:

> Thank you Bob, I spent hours trying to figure this out today and I got  
> it down to commenting out the doctype made errors work - but that just  
> confused me more, simple capitalisation, why is it always the simple  
> things that hurt the most?

Because that’s when you kick yourself the hardest 🙂

---

<div class="post-metadata">

**Author:** ![Ken\_Miller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b2d939/32.png) [@Ken\_Miller](https://www.ruby-forum.com/u/Ken_Miller)\
**Post date:** [January 27, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/has-one-not-working-as-expected/63903/7 "2006-01-27T13:37:34Z")

</div>

Thank you Bob, I spent hours trying to figure this out today and I got  
it down to commenting out the doctype made errors work - but that just  
confused me more, simple capitalisation, why is it always the simple  
things that hurt the most?

Bob S. wrote:

> I’m using the supplied scaffold.css file and in my layout I have a  
> transitional doctype:
> 
> Using this doctype and scaffold.css, when error\_messages\_for() is used,  
> the  
> styles don’t get applied. I believe it is due to the css using  
> #ErrorExplanaion and the actual id/class names are errorExplanation.  
> Note  
> the capitalization of the ‘E’ in error. Fixing the css allowed the  
> styles to  
> be applied to the default output of error\_messages\_for.
> 
> Should this be fixed?
> 
> Thanks.
> 
> Bob S.
