Forum: Ruby on Rails Self-referencial habtm relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0babf8dc63e63a6684706b1ef2e1b691?d=identicon&s=25 Randy Schmidt (Guest)
on 2006-02-21 18:17
(Received via mailing list)
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
Abdf9626d4bdacbe16703056e37215d5?d=identicon&s=25 Jason Cartwright (Guest)
on 2006-02-21 18:44
(Received via mailing list)
Randy Schmidt <x <at> altorg.com> writes:

> 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.
0babf8dc63e63a6684706b1ef2e1b691?d=identicon&s=25 Randy Schmidt (Guest)
on 2006-02-21 18:48
(Received via mailing list)
Thanks! I'll have to give it a try later today.
Randy

On 2/21/06, Jason Cartwright <jcartwright@enspiredsoftware.com> wrote:
> > person_id, friend_id, and approved. How do I control the "approved
> > 267.334.6833
>
> 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
7ed3175d60e44aa7cb891973223e3998?d=identicon&s=25 Chad Fowler (Guest)
on 2006-02-21 22:57
(Received via mailing list)
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!)
0babf8dc63e63a6684706b1ef2e1b691?d=identicon&s=25 Randy Schmidt (Guest)
on 2006-02-22 01:21
(Received via mailing list)
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:
>
> 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
1e99128ebac5ca41c91940291d315332?d=identicon&s=25 Luke Redpath (Guest)
on 2006-02-22 03:27
(Received via mailing list)
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=3...

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:
> > On 2/21/06, Randy Schmidt <x@altorg.com> wrote:
> > Thanks!
> > _______________________________________________
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>



--
Cheers,
Luke Redpath
www.lukeredpath.co.uk
7ed3175d60e44aa7cb891973223e3998?d=identicon&s=25 Chad Fowler (Guest)
on 2006-02-23 06:34
(Received via mailing list)
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!

> 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!)
0babf8dc63e63a6684706b1ef2e1b691?d=identicon&s=25 Randy Schmidt (Guest)
on 2006-02-24 04:13
(Received via mailing list)
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:
>
> 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
This topic is locked and can not be replied to.