Heyo! I am setting up a self-referencial habtm relationship with the users of my app. I am using Chad Fowler''s "Rails Recipes" to get me started, and everything works great with the join table "people_friends". I add friends by doing somebody.friends << somebodyelse. However, with my app, there is an approval process so my join table has columns person_id, friend_id, and approved. How do I control the "approved field"? When I add a friend, I want to set approved to zero, when they are approved, I want to set it to one. I played around with the arrays in console, but all that happened was approved was set to null. If I can''t figure out another way, I am just going to use sql, but I would really like to figure out a better way. Thanks! -- Randy Schmidt x@altorg.com 267.334.6833
Randy Schmidt <x <at> altorg.com> writes:> > Heyo! > > I am setting up a self-referencial habtm relationship with the users > of my app. I am using Chad Fowler''s "Rails Recipes" to get me started, > and everything works great with the join table "people_friends". I add > friends by doing somebody.friends << somebodyelse. However, with my > app, there is an approval process so my join table has columns > person_id, friend_id, and approved. How do I control the "approved > field"? When I add a friend, I want to set approved to zero, when they > are approved, I want to set it to one. I played around with the arrays > in console, but all that happened was approved was set to null. If I > can''t figure out another way, I am just going to use sql, but I would > really like to figure out a better way. > > Thanks! > -- > Randy Schmidt > x <at> altorg.com > 267.334.6833 >In the "Agile Web Development with Rails" book, the example is given on page 232: class User < ActiveRecord::Base has_and_belongs_to_many :articles def read_article(article) articles.push_with_attributes(article, :read_at => Time.now) end end The call to push_with_attributes( ) does all the same work of linking the two models that the << method does, but it also adds the given values to the join table row that it creates every time someone reads an article.
Thanks! I''ll have to give it a try later today. Randy On 2/21/06, Jason Cartwright <jcartwright@enspiredsoftware.com> wrote:> Randy Schmidt <x <at> altorg.com> writes: > > > > > Heyo! > > > > I am setting up a self-referencial habtm relationship with the users > > of my app. I am using Chad Fowler''s "Rails Recipes" to get me started, > > and everything works great with the join table "people_friends". I add > > friends by doing somebody.friends << somebodyelse. However, with my > > app, there is an approval process so my join table has columns > > person_id, friend_id, and approved. How do I control the "approved > > field"? When I add a friend, I want to set approved to zero, when they > > are approved, I want to set it to one. I played around with the arrays > > in console, but all that happened was approved was set to null. If I > > can''t figure out another way, I am just going to use sql, but I would > > really like to figure out a better way. > > > > Thanks! > > -- > > Randy Schmidt > > x <at> altorg.com > > 267.334.6833 > > > > In the "Agile Web Development with Rails" book, the example is given on page 232: > > class User < ActiveRecord::Base > has_and_belongs_to_many :articles > def read_article(article) > articles.push_with_attributes(article, :read_at => Time.now) > end > end > > The call to push_with_attributes( ) does all the same work of linking the two > models that the << method does, but it also adds the given values to the > join table row that it creates every time someone reads an article. > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Randy Schmidt x@altorg.com 267.334.6833
On 2/21/06, Randy Schmidt <x@altorg.com> wrote:> Thanks! I''ll have to give it a try later today. > RandyRandy, you might want to check out the new recipe on Join Models (Many to Many Relationships where there are attributes on the relationship). Join Models are the new favorable way to do many to many "rich" relationships like this. I believe the attributes habtm is on the potentially-deprecate list. Thanks! -- Chad Fowler http://chadfowler.com http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!) http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India, and All I Got Was This Lousy Book) http://rubycentral.org http://rubygarden.org http://rubygems.rubyforge.org (over one million gems served!)
Chad, As soon as Dave Thomas sent an email out that a new beta book of Rails Recipes was out, I went and checked it out and was happy to see that "recipe", if fit what I needed exactly! I''m enjoying the book. Good job so far! Randy On 2/21/06, Chad Fowler <chadfowler@gmail.com> wrote:> On 2/21/06, Randy Schmidt <x@altorg.com> wrote: > > Thanks! I''ll have to give it a try later today. > > Randy > > Randy, you might want to check out the new recipe on Join Models (Many > to Many Relationships where there are attributes on the relationship). > Join Models are the new favorable way to do many to many "rich" > relationships like this. I believe the attributes habtm is on the > potentially-deprecate list. > > > Thanks! > > -- > Chad Fowler > http://chadfowler.com > http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!) > http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India, > and All I Got Was This Lousy Book) > http://rubycentral.org > http://rubygarden.org > http://rubygems.rubyforge.org (over one million gems served!) > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Randy Schmidt x@altorg.com 267.334.6833
Hi Chad, don''t know if you got my email but thanks again for the mention. I noticed you used most of the code that I posted here: http://www.sitepoint.com/forums/showthread.php?t=309718&page=1&pp=15 I was just wondering why you chose to leave out the conditional on the delete callback: def remove_reverse_association(associated_user) associated_user.known_users.delete(self) if associated_user.known_users.include?(self) end But in the beta book: def remove_reverse_association(associated_user) associated_user.known_users.delete(self) end Did you miss it something or did I get it wrong and its simply not needed? On 2/22/06, Randy Schmidt <x@altorg.com> wrote:> Chad, > > As soon as Dave Thomas sent an email out that a new beta book of Rails > Recipes was out, I went and checked it out and was happy to see that > "recipe", if fit what I needed exactly! I''m enjoying the book. Good > job so far! > > Randy > > On 2/21/06, Chad Fowler <chadfowler@gmail.com> wrote: > > On 2/21/06, Randy Schmidt <x@altorg.com> wrote: > > > Thanks! I''ll have to give it a try later today. > > > Randy > > > > Randy, you might want to check out the new recipe on Join Models (Many > > to Many Relationships where there are attributes on the relationship). > > Join Models are the new favorable way to do many to many "rich" > > relationships like this. I believe the attributes habtm is on the > > potentially-deprecate list. > > > > > > Thanks! > > > > -- > > Chad Fowler > > http://chadfowler.com > > http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!) > > http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India, > > and All I Got Was This Lousy Book) > > http://rubycentral.org > > http://rubygarden.org > > http://rubygems.rubyforge.org (over one million gems served!) > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Randy Schmidt > x@altorg.com > 267.334.6833 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers, Luke Redpath www.lukeredpath.co.uk
On 2/21/06, Luke Redpath <contact@lukeredpath.co.uk> wrote:> Hi Chad, don''t know if you got my email but thanks again for the mention. >Oh I sure didn''t! Sorry about that! And thanks to you!> I noticed you used most of the code that I posted here: > > http://www.sitepoint.com/forums/showthread.php?t=309718&page=1&pp=15 > > I was just wondering why you chose to leave out the conditional on the > delete callback: > > def remove_reverse_association(associated_user) > associated_user.known_users.delete(self) if > associated_user.known_users.include?(self) > end > > But in the beta book: > > def remove_reverse_association(associated_user) > associated_user.known_users.delete(self) > end > > Did you miss it something or did I get it wrong and its simply not needed? > >Nothing wrong necessarily, but if you delete something that doesn''t already exist, it''s basically a no-op so the conditional isn''t strictly necessary. Mostly I just try to keep the recipes as simple as possible, so I remove whatever code I can if it''s not completely central to teaching the concept. Thanks! -- Chad Fowler http://chadfowler.com http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!) http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India, and All I Got Was This Lousy Book) http://rubycentral.org http://rubygarden.org http://rubygems.rubyforge.org (over one million gems served!)
Chad, I''ve been working on making a self-referencial habtm using a join model. I got each working independently, but I can''t get the two recipes to work together. here are my models: class User < ActiveRecord::Base has_many :friends has_and_belongs_to_many :acquaintances, :through => :friends, :class_name => "User", :join_table => "friends", :association_foreign_key => "acquaintance_id", :foreign_key => "user_id" end class Friend < ActiveRecord::Base belongs_to :users belongs_to :acquaintances end I tried it with and without an id column in the friends table. Any help would be greatly appreciated! Thanks! Randy On 2/21/06, Chad Fowler <chadfowler@gmail.com> wrote:> On 2/21/06, Randy Schmidt <x@altorg.com> wrote: > > Thanks! I''ll have to give it a try later today. > > Randy > > Randy, you might want to check out the new recipe on Join Models (Many > to Many Relationships where there are attributes on the relationship). > Join Models are the new favorable way to do many to many "rich" > relationships like this. I believe the attributes habtm is on the > potentially-deprecate list. > > > Thanks! > > -- > Chad Fowler > http://chadfowler.com > http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!) > http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India, > and All I Got Was This Lousy Book) > http://rubycentral.org > http://rubygarden.org > http://rubygems.rubyforge.org (over one million gems served!) > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Randy Schmidt x@altorg.com 267.334.6833