SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Location
    Marin CA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    has_and_belongs_to_many OR self-joining association

    I have a basic set up question for my nutrition application:

    The application will have users who log in and receive MEAL prescriptions for the upcoming week. Tables can include:

    User
    Food
    Recipe (perhaps Meal)

    1. I can have a has_and_belongs_to_many relationship between "Food" and "Recipe". Many recipes comprised of many foods.

    OR

    2. I can use a self-joining association
    class Meal < ActiveRecord::Base
    has_many :foods, :class_name => "Meal",
    :foreign_key => "recipe_id"
    belongs_to :recipe, :class_name => "Meal"
    end
    Finally:
    I must remember the "User"... any need to consider polymorphic associations? The "User" will be prescribed "Meals" or "Recipes" on a weekly basis.

    Thanks in advance.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,445
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I can grasp the concept of a many to many relationship between user <=> food, but I'm confused about the relationship between "food" and "recipe/meal". Can you give some examples?

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Location
    Marin CA
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes thank you:

    User A (small person) eats Meal A comprising Foods A,B&C

    User B (larger person) eats Meal B comprising Foods 2*(A,B&C)

    pretty sure i need a has_and_belongs_to_many for

    User <=> Meal
    Meal <=> Food

    Can I do this between two different tables, where Meal is used 2x?

    eg. do I just use two different join tables??? and away I go?


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
  •