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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 9:11 PM, Steve Castaneda
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> 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.
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Jamey Cribbs 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, > > JameyThat 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jan 29, 2010 at 10:52 PM, Steve Castaneda <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Jamey Cribbs 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Steve Castaneda wrote:> Jamey Cribbs 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.