SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Association Assistance

    I'm looking for some assistance in defining the associations for a project I am working on and would be very grateful for some help. I have the following tables:

    Reports
    =============
    id | name


    Sections
    =============
    id | name


    A report can have multiple sections and the user needs to be able to specify the order in which sections will appear in a report. Traditionally I would have added a third table as follows:

    Reports_Sections
    ==================================
    report_id | section_id | user_order


    I can easily set this up using has_and_belongs_to_many, but how would I go about setting the user_order? Is this the correct association or should I be using a has_many :through? If so, how would I then go about setting the user_order?

    Thanks

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Use has_many :through. I'd rename the tables slightly though:
    Code:
    Reports
    =============
    id | name
    
    
    Sections
    =============
    id | name
    
    
    listings
    ==================================
    report_id | section_id | user_order
    Then your models:
    Code:
    class Report < ActiveRecord::Base
      has_many :listings, :dependent => true
      has_many :sections, :through => :listings
    end
    
    class Section < ActiveRecord::Base
      has_many :listings, :dependent => true
      has_many :reports, :through => :listings
    end
    
    class Listing < ActiveRecord::Base
      belongs_to :report
      belongs_to :section
    end
    Now to show a few examples:
    Code:
    #create sections and reports
    home_section = Section.create(:name => 'home')
    work_section = Section.create(:name => 'work')
    tps_report = Report.create(:name => 'TPS Report')
    
    #bind report to sections
    Listing.create(:report => tps_report, :section => work_section, :user_order => 1)
    Listing.create(:report => tps_report, :section => home_section, :user_order => 560)
    
    # Access user order given report and section IDs
    report = Report.find(1)
    puts report.listings.find_by_section_id(1).user_order
    
    #get a count of how many different sections this report is listed in
    puts report.sections.size

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vinnie

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


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
  •