# Belongs\_to and lookup tables

**URL:** <https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168>\
**Category:** Rails\
**Created:** [April 7, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168 "2006-04-07T17:30:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hatless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/848f3c/32.png) [@hatless](https://www.ruby-forum.com/u/hatless)\
**Post date:** [April 7, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168/1 "2006-04-07T17:30:03Z")

</div>

I have two classes, Listing and Warranty. A Listing (think of it as a  
product) may or may not have a Warranty from a lookup table.

When I want the Warranty.company for a given Listing I want to say the  
following:

@listing = Listing.find(4)  
@company = @listing.warranty.company

In order to do that I did the following:

class Listing \< ActiveRecord::Base  
belongs\_to :warranty  
end

and of course my Listing table has a warranty\_id column.

This feels wrong. For one thing, it implies a parent-child relationship  
and implies that any Listings with a given Warranty can/should be  
deleted when a Warranty is deleted. Secondly, when I _do_ delete a  
Warranty, my Listing’s warranty\_id still points to the deleted record’s  
ID, when it should instead be set to nil.

Is there a more appropriate way to express the relationship between a  
class and a lookup table?

---

<div class="post-metadata">

**Author:** ![hatless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/848f3c/32.png) [@hatless](https://www.ruby-forum.com/u/hatless)\
**Post date:** [April 7, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168/2 "2006-04-07T17:50:27Z")

</div>

I believe that (depending on your DBMS) what having an “ON DELETE SET  
NULL” in the foreign key specification of the Listing table schema  
should allow your DBMS to take care of this. This is of course not a  
Rails solution.

A raisl solution could use the “on\_delete” hook (if i’m not making it  
up, im still quite new to Rails myself), and do something like:

def on\_delete  
self.listings.each do { |listing|  
listing.warranty\_id = null  
}  
end

There may of course be a way to do this to all the objects at once  
without using a block but liek I said I am new.

Hope these ideas can help you.

James A.s

---

<div class="post-metadata">

**Author:** ![hatless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/848f3c/32.png) [@hatless](https://www.ruby-forum.com/u/hatless)\
**Post date:** [April 7, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168/3 "2006-04-07T17:53:14Z")

</div>

Steve  
\> class Listing \< ActiveRecord::Base  
\> belongs\_to :warranty  
\> and of course my Listing table has a warranty\_id column.

> This feels wrong.

When you read ‘belongs\_to’, think ‘references’, ‘points\_to’. It won’t  
feel wrong anymore.

Alain

---

<div class="post-metadata">

**Author:** ![hatless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/848f3c/32.png) [@hatless](https://www.ruby-forum.com/u/hatless)\
**Post date:** [April 7, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168/4 "2006-04-07T20:59:17Z")

</div>

It’s not the name that makes it feel wrong. It’s the secondary behaviors  
those terms imply that makes it feel wrong. “references” and “points\_to”  
wouldn’t imply that “child” records could be deleted safely like  
“belongs\_to/has\_many” does. 😉

Alain R. wrote:

> Steve  
> \> class Listing \< ActiveRecord::Base  
> \> belongs\_to :warranty  
> \> and of course my Listing table has a warranty\_id column.
> 
> > This feels wrong.
> 
> When you read ‘belongs\_to’, think ‘references’, ‘points\_to’. It won’t  
> feel wrong anymore.
> 
> Alain

---

<div class="post-metadata">

**Author:** ![hatless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/848f3c/32.png) [@hatless](https://www.ruby-forum.com/u/hatless)\
**Post date:** [April 7, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/belongs-to-and-lookup-tables/57168/5 "2006-04-07T20:54:00Z")

</div>

Thanks. The latter sounds close to a sensible approach, though the  
callback I need is before\_destroy and after setting the property to nil  
it needed a save().

I’ll stick with this unless a better answer comes up regarding my  
discomfort with using belongs\_to for a relationship where I absolutely  
don’t ever want a cascading delete to be implied. IMO there really  
should be separate associations like “refers\_to” and “referenced\_by” to  
express the needs of a lookup table, even if all they essentially do is  
(1) override :dependent to return false at all times and (2) on deletion  
of a lookup table entry, nil out the fields in referring tables that  
pointed to the deleted entry.

Here’s the relevant code I went with. It appears to work:

in listing.rb:

class Listing \< ActiveRecord::Base  
belongs\_to :warranty  
end

in warranty.rb:

class Warranty \< ActiveRecord::Base  
has\_many :listings

```
before_destroy :disassociate_listings

private
  def disassociate_listings
    self.listings.each { |listing|
      listing.warranty_id = nil
      listing.save
    }
  end

```

end

James A.s wrote:

> I believe that (depending on your DBMS) what having an “ON DELETE SET  
> NULL” in the foreign key specification of the Listing table schema  
> should allow your DBMS to take care of this. This is of course not a  
> Rails solution.
> 
> A raisl solution could use the “on\_delete” hook (if i’m not making it  
> up, im still quite new to Rails myself), and do something like:
> 
> def on\_delete  
> self.listings.each do { |listing|  
> listing.warranty\_id = null  
> }  
> end
> 
> There may of course be a way to do this to all the objects at once  
> without using a block but liek I said I am new.
> 
> Hope these ideas can help you.
> 
> James A.s
