# Best way to do this

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-this/182570>\
**Category:** Rails\
**Created:** [January 30, 2010, 3:11am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570 "2010-01-30T03:11:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 3:11am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/1 "2010-01-30T03:11:03Z")

</div>

I’m looking to give an object (buyer) a status. What’s the best way to  
accomplish this? At the moment, I’m struggling to comprehend my way  
through the following:

1. Create a status model, then let the user add statuses as they see  
fit. These statuses can then be applied to a buyer. The buyer table  
will have a status\_id column. This works fine, but I have a problem when  
I try to refer to the the status through a buyer.

buyer.status gives me the following error:

Unknown column ‘statuses.buyer\_id’ in ‘where clause’: SELECT \* FROM  
`statuses` WHERE (`statuses`.buyer\_id = 12) LIMIT 1

I see what it’s trying to do - go into the status column and find all  
statuses that have the buyer\_id assigned. The problem is that I don’t  
want a status to apply to only ONE buyer, I want them to be able to  
apply to multiple.

I’m sure I’m missing something in my routes, or just not understanding  
the relationship and how I need to instruct rails of this.

Can someone help me out? Thanks in advance for any time spent helping.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 3:15am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/2 "2010-01-30T03:15:19Z")

</div>

I’m beginning to think there needs to be a third table; one whose  
columns are:

id, buyer\_id, status\_id

This would be my first time doing it like this though, so I sort of get  
stuck understanding how the route is supposed to work.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 4:31am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/3 "2010-01-30T04:31:01Z")

</div>

Sounds like you do not have your associations defined correctly in your  
model.

In your Buyer model you should have this line:

```
belongs_to :status

```

In your Status model you should have this line:

```
has_many :buyers

```

HTH,

Jamey

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 9:13am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/4 "2010-01-30T09:13:02Z")

</div>

On Fri, Jan 29, 2010 at 10:52 PM, Steve C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > has_many :buyers
> > 
> > ```
> 
> Can you help me understand why it’s belongs\_to and not has\_one? A  
> status is part of a buyer, so I guess I just get confused on the logic.

The big difference between has\_one and belongs\_to is that ActiveRecord  
expects the model that uses the belongs\_to to have a foreign key  
identifying the associated record in the other table. So, therefore  
you know you need to use a belongs\_to in Buyer because that the buyers  
table is where you have defined the status\_id field.

HTH,

Jamey

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 2:30pm UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/5 "2010-01-30T14:30:36Z")

</div>

Steve C. wrote:

> Jamey C. wrote:
> 
> > Sounds like you do not have your associations defined correctly in your  
> > model.
> > 
> > In your Buyer model you should have this line:
> > 
> > ```
> > belongs_to :status
> > 
> > ```
> > 
> > In your Status model you should have this line:
> > 
> > ```
> > has_many :buyers
> > 
> > ```
> > 
> > HTH,
> > 
> > Jamey
> 
> That was it! Thank you so much. I was totally getting confused here:
> 
> belongs\_to :status vs. has\_one :status
> 
> Can you help me understand why it’s belongs\_to and not has\_one? A  
> status is part of a buyer, so I guess I just get confused on the logic.

has\_one is just like has\_many except for different cardinality: it means  
that this table doesn’t contain the foreign key.

If you had read the docs for the Associations module, you would have  
seen this very point clearly explained.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [January 30, 2010, 4:52am UTC](https://www.ruby-forum.com/t/best-way-to-do-this/182570/6 "2010-01-30T04:52:58Z")

</div>

Jamey C. wrote:

> Sounds like you do not have your associations defined correctly in your  
> model.
> 
> In your Buyer model you should have this line:
> 
> ```
> belongs_to :status
> 
> ```
> 
> In your Status model you should have this line:
> 
> ```
> has_many :buyers
> 
> ```
> 
> HTH,
> 
> Jamey

That was it! Thank you so much. I was totally getting confused here:

belongs\_to :status vs. has\_one :status

Can you help me understand why it’s belongs\_to and not has\_one? A  
status is part of a buyer, so I guess I just get confused on the logic.
