First, I am very new to ruby and rails. Searching for "references" is really tough, and so I''ve finally come here. I''m trying to create a scaffold. Let''s say a Business and a School both can have a single Address (not shared). In a different case, an Offer has an OfferType (with just a type_id and description). Also, a Business can have many Offers. (I''ve stripped off most of the unrelated columns, like last_name, etc...) I''m wondering (in about this order): a) Am I doing this right? b) What the difference would be if I used Address business:belongs_to user:belongs_to instead of references? c) Is the Business address:has_one redundant with Address business:references? If not, what does it do? What about the has_many? d) Will this generate join tables for me? And I don''t need to specify xxxxx_id''s anywhere in these relationships? ./script/generate scaffold Business address:has_one offer:has_many name:string first_name:string ./script/generate scaffold School address:has_one name:string ./script/generate scaffold Address business:references school:references address1:string ./script/generate scaffold Offer business:references offerType:has_one start_date:datetime ./script/generate scaffold OfferType image:has_one name:string description:text (I''m on Ruby 1.8.7 and Rails 2.3.8.) Thanks! -- 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.
Kevin Hastie
2010-Jun-03 23:01 UTC
Re: difference between belongs_to and references keyword?
Kevin Hastie wrote:> a) Am I doing this right?Apparently not. Weird. Is it because I didn''t do polymorphic anywhere that Address now has a business_id, a credit_card_id and a user_id? Obviously this is no good - I''d prefer the Business table to have an address_id, etc.... So: e) Did I do this backwards? Should it be Business address:references (or address:belongs_to?) and Address business:has_one? f) Or should I just drop the :references from Addresses and go from there? g) Is using :references (which I don''t see in many docs/tutorials) just going to get me in trouble as a beginner? Thanks again! -- 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.
Kevin Hastie
2010-Jun-03 23:18 UTC
Re: difference between belongs_to and references keyword?
Kevin Hastie wrote: I guess what I really want is something like this: class Address< ActiveRecord::Base belongs_to :addressable, :polymorphic => true end class Business< ActiveRecord::Base has_one :address, :as => :addressable end class School < ActiveRecord::Base has_one :address, :as => :addressable end h) I don''t suppose there is a way to do that with the scaffold. Is there a typical set of scaffold options that would map to this? i) Lastly, I guess I am better off avoiding polymorphism at the beginning, huh? I should just be doing a simple mapping... -- 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.
Ugis Ozols
2010-Jun-04 06:05 UTC
Re: difference between belongs_to and references keyword?
I''ve written a short example about has_many, belongs_to - http://gist.github.com/425026 has_many, has_one, belongs_to deffinitions goes into model and not in scaffold. You can use "references" like this: script/generate scaffold address company:references address:text but as I mentioned in this discussion http://railsforum.com/viewtopic.php?id=39325 (look for andain''s posts) I''m running into problems where views are generated without appended _id. Maybe I''m just doing something wrong or just not getting it right ... Anyway I hope i shed some light on your problem. On Jun 4, 2:18 am, Kevin Hastie <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Kevin Hastie wrote: > > I guess what I really want is something like this: > > class Address< ActiveRecord::Base > belongs_to :addressable, :polymorphic => true > end > > class Business< ActiveRecord::Base > has_one :address, :as => :addressable > end > > class School < ActiveRecord::Base > has_one :address, :as => :addressable > end > > h) I don''t suppose there is a way to do that with the scaffold. Is > there a typical set of scaffold options that would map to this? > > i) Lastly, I guess I am better off avoiding polymorphism at the > beginning, huh? I should just be doing a simple mapping... > -- > Posted viahttp://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.
Ugis Ozols
2010-Jun-04 12:04 UTC
Re: difference between belongs_to and references keyword?
Correction to my previuos message: you can use "belongs_to" and "references" as column types in scaffold (and migrations). They do same thing: add belongs_to association to model. On Jun 4, 9:05 am, Ugis Ozols <ugis.ozo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve written a short example about has_many, belongs_to -http://gist.github.com/425026 > > has_many, has_one, belongs_to deffinitions goes into model and not in > scaffold. You can use "references" like this: script/generate scaffold > address company:references address:text > > but as I mentioned in this discussionhttp://railsforum.com/viewtopic.php?id=39325 > (look for andain''s posts) I''m running into problems where views are > generated without appended _id. Maybe I''m just doing something wrong > or just not getting it right ... > > Anyway I hope i shed some light on your problem. > > On Jun 4, 2:18 am, Kevin Hastie <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > Kevin Hastie wrote: > > > I guess what I really want is something like this: > > > class Address< ActiveRecord::Base > > belongs_to :addressable, :polymorphic => true > > end > > > class Business< ActiveRecord::Base > > has_one :address, :as => :addressable > > end > > > class School < ActiveRecord::Base > > has_one :address, :as => :addressable > > end > > > h) I don''t suppose there is a way to do that with the scaffold. Is > > there a typical set of scaffold options that would map to this? > > > i) Lastly, I guess I am better off avoiding polymorphism at the > > beginning, huh? I should just be doing a simple mapping... > > -- > > Posted viahttp://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.
Kevin Hastie
2010-Jun-10 19:22 UTC
Re: difference between belongs_to and references keyword?
Kevin Hastie wrote:> Kevin Hastie wrote: > > I guess what I really want is something like this: > > class Address< ActiveRecord::Base > belongs_to :addressable, :polymorphic => true > end > > class Business< ActiveRecord::Base > has_one :address, :as => :addressable > end > > class School < ActiveRecord::Base > has_one :address, :as => :addressable > end > > h) I don''t suppose there is a way to do that with the scaffold. Is > there a typical set of scaffold options that would map to this? > > i) Lastly, I guess I am better off avoiding polymorphism at the > beginning, huh? I should just be doing a simple mapping...What about the polymorphic example I gave? It seems like this is the best practice approach, but I am finding it VERY hard to find examples that I can get to work, and I feel like I see lots of complaints that the feature is "broken" within Rails. Is it only for has_many relationships or can it be used like I am trying with has_one relationships? Is my example the correct way to go about it? If so, what else needs to happen? (and what about questions h and i?) j) What seems much easier is to have the Business and School tables to each have an address_id, and be done with it. That''s what I would have done days ago were I programming a language without so many "handy" shortcuts. But to do so, doesn''t it have to be Address has_one :business has_one :school Business belongs_to :address That doesn''t seem right..? -- 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.
sir ziggles
2010-Jun-12 06:44 UTC
Re: difference between belongs_to and references keyword?
i am interested in the answer to this as well. please post if you find out. -- 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.
Marnen Laibow-Koser
2010-Jun-14 18:13 UTC
Re: difference between belongs_to and references keyword?
Kevin Hastie wrote: [...]> j) What seems much easier is to have the Business and School tables to > each have an address_id, and be done with it.That''s correct.> That''s what I would have > done days ago were I programming a language without so many "handy" > shortcuts. But to do so, doesn''t it have to be > > Address > has_one :business > has_one :school > > Business > belongs_to :address > > That doesn''t seem right..?That''s only right if you want an address to treat associated businesses and schools differently. If you just want an address to have_one (or more likely many) associated generic entities, then you need polymorphism. 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.