Attributes of a relationship

View: New views
2 Messages — Rating Filter:   Alert me  

Attributes of a relationship

by Tom Lianza :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/
 

_______________________________________________
Rails mailing list
Rails@...
http://lists.rubyonrails.org/mailman/listinfo/rails

Re: Attributes of a relationship

by j`ey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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__)

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
Rails@...
http://lists.rubyonrails.org/mailman/listinfo/rails