OK I AM new to RoR, and I have more experience with Java and straight
SQL. So maybe I’m just so used to thinking in a way that is
anticipating more than “least” surprise. Anyway, here’s what
If I have an object that belongs to another object, I would expect the
parent to have the FK to the child, not the other way around.
Why? Because if I were to do it in memory (forget about persistence
for a minute), it would like kinda like this:
p = Parent.new
p.children << Child.new
Now in this example, p has an array of children (as in, it HAS MANY).
If I wanted to do a two-way relationship, I would do:
p = Parent.new
c = Child.new
p.children << c
c.parent = p
In this case, the parent has an array of children (literally a list of
many references to its children), and the child has a single reference
to a parent (assuming an xml-like hierarchy here, not a family-tree
Anyway, when I create a “has_one” association, I would expect this
object’s table to literally have a FK column for the “child” object.
When I create a “belongs_to” association, I would expect the same
When I create a “has_many” association, I would expect a fact table
“parent_child” to be created (with id, parent_id, and child_id
columns), and if I wanted a two-way relationship, I would add
“belongs_to” to the Child.
What’s my point?
Glad you asked.
If I wanted to create a class that maintains various user lists:
class UserLists < ActiveRecord::Base
has_many :naughty, :class => “User”
has_many :nice, :class => “User”
has_many :absent, :class => “User”
I would expect some fact tables to be created: naughty_users (with
columns id, userlist_id, user_id), nice_users (with id, userlist_id,
user_id), and absent_users (again with id, userlist, and id).
Could someone set my thinking straight here? Maybe I just need a
little explanation around best practices or something…