Folks Can a model just have a belongs_to only? or do you have to have a belongs_to and a has_many or has_one on the other model? -- 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.
Of course you can. On Sunday, 27 May 2012 20:34:44 UTC+3, Ruby-Forum.com User wrote:> > Folks > Can a model just have a belongs_to only? or do you have to have a > belongs_to and a has_many or has_one on the other model? > > -- > 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 view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V_d2qxNa_TUJ. 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 Sun, May 27, 2012 at 1:34 PM, brent brent <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Folks > Can a model just have a belongs_to only? or do you have to have a > belongs_to and a has_many or has_one on the other model? > > --http://guides.rubyonrails.org/association_basics.html#the-belongs_to-association -- Greg Akins http://twitter.com/akinsgre -- 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.
What belongs_to and their counterparts does is create a method to access an existing relationship, the actual relationship is made in the tables themselves by adding model_id columns, for instance: class User < ActiveRecord::Base has_one :address end class Address < ActiveRecord::Base belongs_to :user end # Shows the address whose addresses.user_id = users.id User.last.address # Shows the user whose users.id = addresses.user_id Address.last.user If you don''t need to access the relationship in the Address model just delete `belongs_to :user`: class Address < ActiveRecord::Base end User.last.address # Shows address Address.last.user # NoMethodError: undefined method `user'' On Sunday, May 27, 2012 12:34:44 PM UTC-5, Ruby-Forum.com User wrote:> > Folks > Can a model just have a belongs_to only? or do you have to have a > belongs_to and a has_many or has_one on the other model? > > -- > 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 view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/21FdWS8FBKUJ. 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.