Best way to do this

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.

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.

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

On Fri, Jan 29, 2010 at 10:52 PM, Steve C. [email protected]
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

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
[email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs