# Belongs\_to association doesn't work with \_id set

**URL:** <https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643>\
**Category:** Rails\
**Created:** [October 8, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643 "2007-10-08T12:01:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/1 "2007-10-08T12:01:12Z")

</div>

Hello there,  
I’m developing an application which has models like this:

class Vehicle \< ActiveRecord::Base  
belongs\_to :internal\_dim, :class\_name =\> “Dimension”

```
def before_create()
    idim = Dimension.create
    self.internal_dim_id = idim.id
    #self.create_internal_dim # Doesn't work
end

```

end

class Dimension \< ActiveRecord::Base  
has\_one :vehicle  
end

That is, creating the vehicle should create a dimension entry (yeah, i  
know it’s strange, but actually dimension table is shared among  
different things, so the relationship is inverted).

Anyway, it happens that when a vehicle is created, the internal  
dimension id is correctly set:

> > > v = Vehicle.create  
> > > v.internal\_dim\_id  
> > > =\> 3

But the method doesn’t get the corresponding value:

> > > v.internal\_dim  
> > > =\> nil

While the records exists:

> > Dimension.find(v.internal\_dim\_id)  
> > =\> #\<Dimension:0xb7042678 @attributes={blah}\>

Have you any idea?  
Thanks

~~Ale

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/2 "2007-10-08T12:53:55Z")

</div>

Alessandro Re wrote:

> Hello there,  
> I’m developing an application which has models like this:
> 
> class Vehicle \< ActiveRecord::Base  
> belongs\_to :internal\_dim, :class\_name =\> “Dimension”
> 
> ```
> def before_create()
> idim = Dimension.create
> self.internal_dim_id = idim.id
> #self.create_internal_dim # Doesn't work
> end
> 
> ```
> 
> end
> 
> class Dimension \< ActiveRecord::Base  
> has\_one :vehicle  
> end
> 
> That is, creating the vehicle should create a dimension entry (yeah, i  
> know it’s strange, but actually dimension table is shared among  
> different things, so the relationship is inverted).
> 
> Anyway, it happens that when a vehicle is created, the internal  
> dimension id is correctly set:
> 
> > > > v = Vehicle.create  
> > > > v.internal\_dim\_id  
> > > > =\> 3
> 
> But the method doesn’t get the corresponding value:
> 
> > > > v.internal\_dim  
> > > > =\> nil
> 
> While the records exists:
> 
> > > Dimension.find(v.internal\_dim\_id)  
> > > =\> #\<Dimension:0xb7042678 @attributes={blah}\>
> 
> Have you any idea?

It’s because of the has\_one in Dimension. It causes the reference to be  
held by the other object. Either change it into has\_many or move the  
foreign key column over to the Dimension table.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/3 "2007-10-08T13:24:47Z")

</div>

Roderick van Domburg wrote:

> It’s because of the has\_one in Dimension. It causes the reference to be  
> held by the other object. Either change it into has\_many or move the  
> foreign key column over to the Dimension table.

Argh! You’re right!  
Thanks very much 🙂

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/4 "2007-10-08T15:10:16Z")

</div>

Alessandro Re wrote:

> Roderick van Domburg wrote:
> 
> > It’s because of the has\_one in Dimension. It causes the reference to be  
> > held by the other object. Either change it into has\_many or move the  
> > foreign key column over to the Dimension table.
> 
> Argh! You’re right!  
> Thanks very much 🙂

No, sorry. It doesn’t work with has\_many ☹  
And I can’t move the foreign key into Dimension table, so I must find  
something else.

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/5 "2007-10-08T15:15:19Z")

</div>

On 8 Oct 2007, at 11:01, Alessandro Re wrote:

> ```
> #self.create_internal_dim # Doesn't work
> end
> 
> ```
> 
> end

Why not self.internal\_dim = idim ?

Fred

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/6 "2007-10-08T15:24:32Z")

</div>

Frederick C. wrote:

> On 8 Oct 2007, at 11:01, Alessandro Re wrote:
> 
> > ```
> > #self.create_internal_dim # Doesn't work
> > end
> > 
> > ```
> > 
> > end
> 
> Why not self.internal\_dim = idim ?
> 
> Fred

I found out that it doesn’t work: after the assignment the  
internal\_dim\_id is nil.

Look: the actual code:

def before\_create()  
idim = Dimension.create  
edim = Dimension.create  
self.internal\_dim\_id = idim.id  
self.external\_dim\_id = edim.id

```
puts(" Dimensions: int #{internal_dim_id} ext #{external_dim_id}")

```

end

output:

#Company:0xb711d494 created  
#Dimension:0xb70d02e8 created  
#Dimension:0xb70c8480 created  
Dimensions: int 1 ext 2

Changing with:

def before\_create()  
idim = Dimension.create  
#edim = Dimension.create  
self.internal\_dim\_id = idim.id  
#self.external\_dim\_id = edim.id  
#self.create\_internal\_dim  
self.create\_external\_dim  
puts(" Dimensions: int #{internal\_dim\_id} ext #{external\_dim\_id}")  
end

output:

#Company:0xb713510c created  
#Dimension:0xb70ebd04 created  
#Dimension:0xb70e5634 created  
Dimensions: int 1 ext

As you see, external dim id is not set, even if the dimension is  
created. I didn’t figure out why this happen, yet.  
But this is a minimal issue for me now, I need to understand before why  
internal\_dim doesn’t return the associated dimension, even if the id is  
set.

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/7 "2007-10-08T16:21:06Z")

</div>

Ok, solved. The problem was that

> class Vehicle \< ActiveRecord::Base  
> belongs\_to :internal\_dim, :class\_name =\> “Dimension”

doesn’t set automatically the foreign key to internal\_dim\_id when  
class\_name is used. So, with

> ```
> belongs_to :internal_dim, :foreign_key => "internal_dim_id", :class_name => "Dimension"
> 
> ```

it works, and probably (didn’t try yet), it will fix the assignment  
problems:

> > ```
> > #self.create_internal_dim # Doesn't work
> > 
> > ```
> 
> Why not self.internal\_dim = idim ?

and also

> > self.create\_intternal\_dim

Thanks guys, hope this will help someone in future

~~Aki

---

<div class="post-metadata">

**Author:** ![akiross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@akiross](https://www.ruby-forum.com/u/akiross)\
**Post date:** [October 8, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/belongs-to-association-doesnt-work-with-id-set/116643/8 "2007-10-08T16:38:53Z")

</div>

On Mon, 8 Oct 2007 16:21:06 +0200, Alessandro Re wrote:

> Ok, solved. The problem was that
> 
> > class Vehicle \< ActiveRecord::Base  
> > belongs\_to :internal\_dim, :class\_name =\> “Dimension”
> 
> doesn’t set automatically the foreign key to internal\_dim\_id when  
> class\_name is used. So, with

IIRC that is fixed with 2.0. Yay.

–  
Jay L. |  
Boston, MA | My character doesn’t like it when they  
Faster: jay at jay dot fm | cry or shout or hit.  
[http://www.jay.fm](http://www.jay.fm) | - Kristoffer
