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.
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.
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.
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.
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.
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.
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!
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!