Forum: Ruby on Rails Attributes of a relationship

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bd8bfdb75fccbe408720178434ded311?d=identicon&s=25 Tom Lianza (Guest)
on 2006-01-15 09:11
(Received via mailing list)
Hi all,

I have two types of models which I'm not sure how to model (or if it's
feasible) with ActiveRecord.  Let me know if you've got any good ideas.

1) For those of you who are familliar with, you probably
that you can bookmark a site, and add your own tags to it.  Each person
their own tags for a site, even though multiple people can bookmark a
How would you create that relationship in ActiveRecord?
  user has many websites (easy)
  BUT... there's no relation between tags and websites, or users and
It's like a 3-way user-tag-website relationship.  "Every user-website
has many tags".  How do you model that one?  Easy enough to do in a
relational db, but I can't get my head around it with Active Record.

2) This one may be easier, but I still can't get it.. kinda similar to
Let's say a user can bookmark a website, and can also "rank" that
Unlike the example above, all I really want to do is assign an attribute
the user-website relationship (ie add a field to the users_websites
"every user-website pair has one rank".  Again - is this one possible?

Many thanks for any advice you can give.  I know these relationships are
somewhat complext, just wondering how far I can take ActiveRecord before
dropping into straight SQL.


6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-01-15 10:07
  has_many :bookmarks

  belongs_to :user
  belongs_to :site
  has_and_belongs_to_many :tags

  has_many :bookmarks

  has_and_belongs_to_many :bookmarks

You would need the following tables:
  bookmarks << (has a user_id and site_id foreign key)(this could also
have a rank field)
  bookmarks_tags << (bookmark_id and tag_id foreign key)

In rails you will then be able to do the following:
  @user = User.find(:first)
  @user.bookmarks (returns list of bookmarks for this user)

  @bookmark = Bookmark.find(:first)
  @bookmark.tags (returns a list of tags)

  @tag = Tag.find(:first)
  @tag.bookmarks (returns list of bookmarks for this tag)

  @site = Site.find(:first)
  @site.bookmarks (returns list of bookmarks for this site)

I hope this helps,
Joey (joey__)
This topic is locked and can not be replied to.