sfnatureguy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2012-Dec-31 00:59 UTC
Help with has_many through association
I just posted a question on stackoverflow: http://stackoverflow.com/questions/14096008/has-many-through-but-with-unique-source-and-multiple-associations The gist of what I need help with is this: - As a user, I can add my favorite book to my book list. - Another user adds the same book to their book list. - There should only be one instance of the book in the database but 2 user/book associations. Any help would be amazing! -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8xReZsFy1_cJ. For more options, visit https://groups.google.com/groups/opt_out.
On Sun, Dec 30, 2012 at 7:59 PM, <sfnatureguy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> As a user, I can add my favorite book to my book list. > Another user adds the same book to their book list. > > There should only be one instance of the book in the database but 2 > user/book associations.When someone wants to add their favorite, check the database to see if it exists. What''s the problem? -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- 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 https://groups.google.com/groups/opt_out.
sfnatureguy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2013-Jan-01 10:28 UTC
Re: Help with has_many through association
The problem is that once you dive in, all of the "cool" rails scoping (user.books.create...) fails and you start managing the relationships yourself (user.books << book.find_or_create). It feels "wrong", but as I''m diving in deeper it seems like the best solution is to create an intermediate class to handle the association management and keep the logic out of the controllers. On Monday, December 31, 2012 9:26:09 PM UTC-8, Dave Aronson wrote:> > On Sun, Dec 30, 2012 at 7:59 PM, <sfnat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> > wrote: > > > As a user, I can add my favorite book to my book list. > > Another user adds the same book to their book list. > > > > There should only be one instance of the book in the database but 2 > > user/book associations. > > When someone wants to add their favorite, check the database to see if > it exists. What''s the problem? > > -Dave > > -- > Dave Aronson, the T. Rex of Codosaurus LLC, > secret-cleared freelance software developer > taking contracts in or near NoVa or remote. > See information at http://www.Codosaur.us/. >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/X6pe_0eHgdQJ. For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Jan 1, 2013 at 5:28 AM, <sfnatureguy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The problem is that once you dive in, all of the "cool" rails scoping > (user.books.create...) fails and you start managing the relationships > yourself (user.books << book.find_or_create). It feels "wrong", but as I''m > diving in deeper it seems like the best solution is to create an > intermediate class to handle the association management and keep the logic > out of the controllers.What''s wrong with "@user.favorite_books << Book.find_or_create_by_title(@title)"? That''s still some pretty "cool" Rails scoping. Compared to what you''d have to do in raw SQL, it saves you tons of work, and is still very clear. Sure it''s not as simple as "@user.favorite_books.create @title", but hey, if you''ve got more needs, such as non-duplication, often you need to write more code, and in this case the difference is still small. Seems to me that "BookListManager.add_to_favorites_for @user, @title" isn''t any better than that. It just adds one more layer of indirection, one more place to have to go look to find out what''s happening. Of course the "enterprisey" lovers of huge ER diagrams would advocate for that approach (complete with a separate interface class for it to implement), but they can just go pound Java. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- 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 https://groups.google.com/groups/opt_out.