ActiveRecord and Foreign Keys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Vondrick

    ActiveRecord and Foreign Keys

    Hi everyone,
    I am having trouble wrapping my mind around the ActiveRecord pattern and
    foreign keys.

    I understand that for each table, there should be a class, and each row
    is is effectively an instance.

    But, my problem arises when I look at foreign keys and advanced
    relationships. For basic foreign keys, I can just automatically
    reference to the object. Example:

    Table: invoice
    id, notes

    Table: items
    id, cost, invoice_id

    When working with an instance with items, I can easily load a new object
    called "invoice."

    But, what if I am already working with invoice, and I want to load
    items? I can't think of any way to abstractly relate the two.

    Also, what if I want to two things that are both a "has many"
    relationship? For example, if I take users that can have multiple
    qualities, I would need three tables: one for users, one for the
    qualities, and one to link.

    Table: users
    id, username

    Table: link
    id, user_id, quality_id

    table: qualities
    id, quality_name

    How does ActiveRecord adapt to this? Any insight that you have or
    suggestions are appreciated! I can't think of an elegant solution.

    Carl
Working...