Self-referencial habtm relationship


#1

Heyo!

I am setting up a self-referencial habtm relationship with the users
of my app. I am using Chad F.'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 S.
removed_email_address@domain.invalid
267.334.6833


#2

Randy S. <x 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 S.
x altorg.com
267.334.6833

In the “Agile Web D. 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.


#3

Thanks! I’ll have to give it a try later today.
Randy

On 2/21/06, Jason C. removed_email_address@domain.invalid 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Randy S.
removed_email_address@domain.invalid
267.334.6833


#4

On 2/21/06, Randy S. removed_email_address@domain.invalid 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 F.
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!)


#5

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 S. removed_email_address@domain.invalid wrote:

On 2/21/06, Randy S. removed_email_address@domain.invalid wrote:
Thanks!



Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Cheers,
Luke R.
www.lukeredpath.co.uk


#6

On 2/21/06, Luke R. removed_email_address@domain.invalid 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 F.
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!)


#7

Chad,

As soon as Dave T. 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 F. removed_email_address@domain.invalid wrote:

http://rubygems.rubyforge.org (over one million gems served!)


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Randy S.
removed_email_address@domain.invalid
267.334.6833


#8

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 F. removed_email_address@domain.invalid wrote:

http://rubygems.rubyforge.org (over one million gems served!)


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Randy S.
removed_email_address@domain.invalid
267.334.6833