I have a one-to-one relationship between 2 models, should I store each others id on both objects or just in one? Thank you, Rodrigo -- 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.
use has_one and belongs_to and i guess: store each others for maintain integrity is the best way, always Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu:> I have a one-to-one relationship between 2 models, should I store each others id on both objects or just in one? > > Thank you, > > Rodrigo > > > -- > 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.
On Fri, Jul 1, 2011 at 6:50 AM, Leoncio Caminha <leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> use has_one and belongs_to and i guess: store each others for maintain > integrity is the best way, always > Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu: > > > I have a one-to-one relationship between 2 models, should I store each > others id on both objects or just in one? > > >for code readability''s sake, you need to figure out where to put the foreign but not on both. for example, you have a 1-1 relationship between a dog and an owner. it''s better if you add the foreign key to the dogs table. Class Dog < ActiveRecord::Base belongs_to :owner # owner_id is in the dogs table end Class Owner < ActiveRecord::Base has_one :dog end one more thing to consider is through associations. you can''t add a :through option to belongs_to.> > Thank you, > > > > Rodrigo > > > > > > -- > > 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. > > -- > 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. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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.
Well, i think that in the given example, u could put has_one :dog in the Owner Controller and ALSO a belongs_to :owner in the Dog''s controller. That way, you''ll have the relations of both controllers explicit in each of them improving readability. Em 1/7/2011 05:48, Jim Ruther Nill escreveu:> > > On Fri, Jul 1, 2011 at 6:50 AM, Leoncio Caminha > <leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <mailto:leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > use has_one and belongs_to and i guess: store each others for > maintain integrity is the best way, always > Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu: > > > I have a one-to-one relationship between 2 models, should I > store each others id on both objects or just in one? > > > > > for code readability''s sake, you need to figure out where to put the > foreign but not on both. for example, you have a 1-1 > relationship between a dog and an owner. it''s better if you add the > foreign key to the dogs table. > > Class Dog < ActiveRecord::Base > belongs_to :owner # owner_id is in the dogs table > end > > Class Owner < ActiveRecord::Base > has_one :dog > end > > one more thing to consider is through associations. you can''t add a > :through option to belongs_to. > > > Thank you, > > > > Rodrigo > > > > > > -- > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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.-- 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.
On Fri, Jul 1, 2011 at 9:44 PM, VitorHP <vitorhp2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ** > Well, i think that in the given example, u could put has_one :dog in the > Owner Controller and ALSO a belongs_to :owner in the Dog''s controller. That > way, you''ll have the relations of both controllers explicit in each of them > improving readability. > >this is confusing. you can''t call has_one and belongs_to in a controller.> Em 1/7/2011 05:48, Jim Ruther Nill escreveu: > > > > On Fri, Jul 1, 2011 at 6:50 AM, Leoncio Caminha < > leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> use has_one and belongs_to and i guess: store each others for maintain >> integrity is the best way, always >> Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu: >> >> > I have a one-to-one relationship between 2 models, should I store each >> others id on both objects or just in one? >> > >> > > for code readability''s sake, you need to figure out where to put the > foreign but not on both. for example, you have a 1-1 > relationship between a dog and an owner. it''s better if you add the > foreign key to the dogs table. > > Class Dog < ActiveRecord::Base > belongs_to :owner # owner_id is in the dogs table > end > > Class Owner < ActiveRecord::Base > has_one :dog > end > > one more thing to consider is through associations. you can''t add a > :through option to belongs_to. > > > >> > Thank you, >> > >> > Rodrigo >> > >> > >> > -- >> > 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-/JYPxA39Uh5TLH3MbocFFw@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. >> >> -- >> 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. >> >> > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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. > > > -- > 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. >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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.
exact! this is a model task, remember guys, MVC, this is a model task, paper of model. 2011/7/1 Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > > On Fri, Jul 1, 2011 at 9:44 PM, VitorHP <vitorhp2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> ** >> Well, i think that in the given example, u could put has_one :dog in the >> Owner Controller and ALSO a belongs_to :owner in the Dog''s controller. That >> way, you''ll have the relations of both controllers explicit in each of them >> improving readability. >> >> > this is confusing. you can''t call has_one and belongs_to in a controller. > > > >> Em 1/7/2011 05:48, Jim Ruther Nill escreveu: >> >> >> >> On Fri, Jul 1, 2011 at 6:50 AM, Leoncio Caminha < >> leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> use has_one and belongs_to and i guess: store each others for maintain >>> integrity is the best way, always >>> Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu: >>> >>> > I have a one-to-one relationship between 2 models, should I store each >>> others id on both objects or just in one? >>> > >>> >> >> for code readability''s sake, you need to figure out where to put the >> foreign but not on both. for example, you have a 1-1 >> relationship between a dog and an owner. it''s better if you add the >> foreign key to the dogs table. >> >> Class Dog < ActiveRecord::Base >> belongs_to :owner # owner_id is in the dogs table >> end >> >> Class Owner < ActiveRecord::Base >> has_one :dog >> end >> >> one more thing to consider is through associations. you can''t add a >> :through option to belongs_to. >> >> >> >>> > Thank you, >>> > >>> > Rodrigo >>> > >>> > >>> > -- >>> > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm >>> . >>> > 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. >>> >>> -- >>> 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. >>> >>> >> >> >> -- >> ------------------------------------------------------------- >> visit my blog at http://jimlabs.heroku.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. >> >> >> -- >> 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. >> > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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. >-- 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.
Oh Right! I got the names wrong. My bad. xD Em 1/7/2011 11:22, leoncio caminha escreveu:> exact! this is a model task, remember guys, MVC, this is a model task, > paper of model. > > 2011/7/1 Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <mailto:jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > > > > On Fri, Jul 1, 2011 at 9:44 PM, VitorHP <vitorhp2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:vitorhp2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > Well, i think that in the given example, u could put has_one > :dog in the Owner Controller and ALSO a belongs_to :owner in > the Dog''s controller. That way, you''ll have the relations of > both controllers explicit in each of them improving readability. > > > this is confusing. you can''t call has_one and belongs_to in a > controller. > > Em 1/7/2011 05:48, Jim Ruther Nill escreveu: >> >> >> On Fri, Jul 1, 2011 at 6:50 AM, Leoncio Caminha >> <leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org >> <mailto:leoncio.sobreira-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: >> >> use has_one and belongs_to and i guess: store each others >> for maintain integrity is the best way, always >> Em 30/06/2011, às 19:42, Rodrigo Ruiz escreveu: >> >> > I have a one-to-one relationship between 2 models, >> should I store each others id on both objects or just in one? >> > >> >> >> for code readability''s sake, you need to figure out where to >> put the foreign but not on both. for example, you have a 1-1 >> relationship between a dog and an owner. it''s better if you >> add the foreign key to the dogs table. >> >> Class Dog < ActiveRecord::Base >> belongs_to :owner # owner_id is in the dogs table >> end >> >> Class Owner < ActiveRecord::Base >> has_one :dog >> end >> >> one more thing to consider is through associations. you >> can''t add a :through option to belongs_to. >> >> > Thank you, >> > >> > Rodrigo >> > >> > >> > -- >> > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> > To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >> >> >> -- >> ------------------------------------------------------------- >> visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> <mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. >> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > ------------------------------------------------------------- > visit my blog at http://jimlabs.heroku.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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > <mailto:rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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.