SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict JNKlein's Avatar
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    has_and_belongs_to_many: joined attributes

    Using the common Author - Article example:

    Author habtm Articles
    Article habtm Authors
    in authors_articles (the joining table) there is an attribute word_count (how many words in this article were written by this author)

    How would we, then, figure out total_word_count of an author?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that in Rails currently you couldn't access attributes in the join table? I think it'd be better anyways to put word_count in the articles table. And to do a total word count, you could make a method such as this in author:

    Code:
    model Author < ActiveRecord::Base
      ...
      def total_word_count
        word_count = 0
        self.articles.each do { |art| word_count += art.word_count }
        word_count
      end
    end
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Addict JNKlein's Avatar
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats not the same though. That is an author that has_many articles and an article that has_one author.

    I'm talking about an atribute unique to the combination: that is, how many words in THIS article were written by THIS author (assuming multiple authors).

    Is that correct that this is not possible in rails?

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JNKlein
    Thats not the same though. That is an author that has_many articles and an article that has_one author.

    I'm talking about an atribute unique to the combination: that is, how many words in THIS article were written by THIS author (assuming multiple authors).

    Is that correct that this is not possible in rails?
    Ah yes you're correct. As far as I know, you can not store attributes in the many to many join table yet...but someone correct me if im wrong.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  5. #5
    SitePoint Addict JNKlein's Avatar
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a hack or alternate method to accomplish the same thing?

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're adding more fields to authors_articles, you could this the Rails 1.1 feature:

    author:
    has_many :authorships
    has_many :articles, :through => :authorships

    article:
    has_many :authorships
    has_many :authors, :through => :authorships

    authorship:
    belongs_to :author
    belongs_to :article

    You can use SQL to find the total_word_count. Define a method total_word_count for the Author model, and use connection.select_value() + SQL to select the total word count.

  7. #7
    SitePoint Addict JNKlein's Avatar
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very, very cool. Looks like I'll be upgrading to 1.1 right now. Thanks Fenrir2.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •