Hello All I am working on my new project. in that my requriments are like this Subjects have many books, book belongs to subjects Books have many Chapters, Chapter belongs to Books Chapter have many pages, page belongs to Chapters Is that fine or it could be done better Ways Thanks in advance -- 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/-/T35nCJDJ45YJ. 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.
jay.gallivan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2011-Jun-22 17:21 UTC
Re: Best Models Associations suggestions
I like has_many through with a CatalogEntry join model. ## example Page call ## Page.last.chapter.book.subjects.first ## models ## class CatalogEntry < ActiveRecord::Base belongs_to :subject belongs_to :book end class Book < ActiveRecord::Base has_many :catalog_entries has_many :subjects, :through => :catalog_entries has_many :chapters end class Subject < ActiveRecord::Base has_many :catalog_entries has_many :books, :through => :catalog_entries end class Chapter < ActiveRecord::Base belongs_to :book has_many :pages end class Page < ActiveRecord::Base belongs_to :chapter end ## seeds.rb ## subject01 = Subject.create!(:name => ''Subject One'') subject02 = Subject.create!(:name => ''Subject Two'') book01 = Book.create!(:name => ''Book One'') book02 = Book.create!(:name => ''Book Two'') book01_chapter01 = Chapter.create!(:book => book01, :name => ''Chapter One'') book01_chapter02 = Chapter.create!(:book => book01, :name => ''Chapter Two'') book02_chapter01 = Chapter.create!(:book => book02, :name => ''Chapter One'') book02_chapter02 = Chapter.create!(:book => book02, :name => ''Chapter Two'') book01_chapter01_page01 = Page.create!(:chapter => book01_chapter01, :number => 1) book01_chapter02_page02 = Page.create!(:chapter => book01_chapter02, :number => 2) book02_chapter01_page01 = Page.create!(:chapter => book02_chapter01, :number => 1) book02_chapter02_page02 = Page.create!(:chapter => book02_chapter02, :number => 2) book01.subjects << subject01 book01.subjects << subject02 book01.save book02.subjects << subject01 book02.subjects << subject02 book02.save On 06/22/2011 10:08 AM, rajeevkannav wrote:> Hello All > I am working on my new project. in that my requriments are like this > > Subjects have many books, book belongs to subjects > Books have many Chapters, Chapter belongs to Books > Chapter have many pages, page belongs to Chapters > Is that fine or it could be done better Ways > > Thanks in advance > -- > 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/-/T35nCJDJ45YJ. > 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.
On Wednesday, June 22, 2011 1:21:06 PM UTC-4, gallivan wrote:> > I like has_many through with a CatalogEntry join model.Devil''s advocate here... what do you gain in this example by using a has_many :through instead of has_and_belongs_to_many? Is one ever really going to neeed to access a CatalogEntry instance? -- 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/-/AGK7uvDgf-4J. 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.
jay.gallivan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2011-Jun-23 01:01 UTC
Re: Best Models Associations suggestions
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> There are two things I like about has_many :through. First, it causes me to think about and try to name the relationship. Second, it treats the join model as a first class entity, formalizing the relationship - so i can say book.catalog_entries as well as book.subjects.<br> <br> Form me, has_and_belongs_to_many obscures rather than reveals relationships so avoided it.<br> <br> My preference does not define general practice. So let''s look at one authority. From The Rails 3 Way, section 7.5.1.<br> <blockquote><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: ''Times New Roman''; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Verdana,sans-serif; font-size: 15px; text-align: left;"> <p class="docText" style="margin: 0px 0px 12px; padding: 0px; font-size: 16px; line-height: 1.2em; font-weight: normal; color: black; font-family: verdana,sans-serif;">Before proceeding with this section, I must clear my conscience by stating that<span class="Apple-converted-space"> </span><tt style="color: green; font-family: ''Courier New'',Courier,monospace; font-size: 16px;"><span class="docTextHighlight" style="background-color: rgb(255, 226, 152);">has_and_belongs_to_many</span></tt><a name="including the"></a><span class="Apple-converted-space"> </span>is practically obsolete in the minds of many Rails developers, including the authors of this book. Use<span class="Apple-converted-space"> </span><tt style="color: green; font-family: ''Courier New'',Courier,monospace; font-size: 16px;">has_many :through</tt><a name="lot easier"></a><span class="Apple-converted-space"> </span>instead and your life should be a lot easier. The section is preserved in this edition almost exactly as it appeared in the first, because it contains good techniques that enlighten the reader about nuances of Active Record behavior.</p> </span></span></blockquote> That, too, is an opinion. But one probably more broadly founded than mine.<br> <br> As to a ''need'' for CatalogEntry.... You need a join model. HABTM uses a join model. It just elides the fact.<br> <br> Is the devil''s advocate satisfied? <br> <br> On 06/22/2011 01:31 PM, Tim Shaffer wrote: <blockquote cite="mid:10546003.1029.1308767488905.JavaMail.geo-discussion-forums@vbjb24" type="cite">On Wednesday, June 22, 2011 1:21:06 PM UTC-4, gallivan wrote: <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">I like has_many through with a CatalogEntry join model.</blockquote> <div><br> Devil''s advocate here... what do you gain in this example by using a has_many :through instead of has_and_belongs_to_many? Is one ever really going to neeed to access a CatalogEntry instance?<br> </div> -- <br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.<br> To view this discussion on the web visit <a moz-do-not-send="true" href="https://groups.google.com/d/msg/rubyonrails-talk/-/AGK7uvDgf-4J">https://groups.google.com/d/msg/rubyonrails-talk/-/AGK7uvDgf-4J</a>.<br> To post to this group, send email to <a class="moz-txt-link-abbreviated" href="mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org">rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org</a>.<br> To unsubscribe from this group, send email to <a class="moz-txt-link-abbreviated" href="mailto:rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org">rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org</a>.<br> For more options, visit this group at <a class="moz-txt-link-freetext" href="http://groups.google.com/group/rubyonrails-talk?hl=en">http://groups.google.com/group/rubyonrails-talk?hl=en</a>.<br> </blockquote> <br> </body> </html> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.<br /> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org<br /> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.<br />