SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Associations problem in ror

    hi,
    i am new to ror.
    i have some doubt in associations.This is my db follow.


    catalogues table
    id
    name

    catalogue_items

    id
    catalogue_id
    product_id
    position


    products
    name
    price

    I have written the associations as follows for the above tables as

    catalogue model

    has_many:catalogue_item

    products model

    has_many:catalogue_item

    catalogue_item model

    belongs_to:catalogue
    belongs_to:model

    *******************************************
    can i write the above as

    * Catalogue has_many :catalogue_items
    * Catalogue has_many products, :through => :catalogue_items

    plz tell me whether both are correct or not
    http://www.w3answers.com

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it's correct. But you still need the other associations:

    Code:
    catalogue:
      hash_many :catalogue_items
      has_many :products, :through => :catalogue_items
    
    product:
      has_many :catalogue_items
    
    catalogue_item:
      belongs_to :catalogue
      belongs_to :product


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
  •