Forum: Ruby on Rails Attributes of a 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.
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
even
feasible) with ActiveRecord.  Let me know if you've got any good ideas.

1) For those of you who are familliar with del.icio.us, you probably
know
that you can bookmark a site, and add your own tags to it.  Each person
has
their own tags for a site, even though multiple people can bookmark a
site.
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
tags.
It's like a 3-way user-tag-website relationship.  "Every user-website
pair
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
#1.
Let's say a user can bookmark a website, and can also "rank" that
website.
Unlike the example above, all I really want to do is assign an attribute
to
the user-website relationship (ie add a field to the users_websites
table)
"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.

Thanks!
Tom

----
http://lianza.org/
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-01-15 10:07
User:
  has_many :bookmarks

Bookmarks:
  belongs_to :user
  belongs_to :site
  has_and_belongs_to_many :tags

Site:
  has_many :bookmarks

Tags:
  has_and_belongs_to_many :bookmarks

You would need the following tables:
  users
  bookmarks << (has a user_id and site_id foreign key)(this could also
have a rank field)
  sites
  tags
  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.