Hey everyone, I’m really just starting Rails and I posted a day or
two ago with my first question. I think I’m needing to back up here
because something just isn’t seeming right. I am hoping someone
might be kind enough to shed some light on ActiveRecord & the right
way to set things up.
In my little test application I have models for Account, User,
ServicePlan. The idea is that a person joins by first choosing a
service plan, and then chooses an Account Name and a User login
name. After the account is created multiple users can be added to
the account. The options for the account are determined by the
service plan, so someone on Plan 1 might get 20 downloads a month,
while Plan 5 might get unlimited downloads.
Now as far as object relations go, an Account “has_many” users, which
works fine. The problem however comes in when I start under the
assumption that an Account “has_one” service plan. Rails doesn’t
error, & my validations pass, but nothing is saved to the DB table
(I’m still learning the unit test stuff so my tests aren’t even
written, let alone pass so they give me no help yet).
The problem is there should be (as per good DB design) only one
record per service plan, so I’m starting to think that Account
“has_one” ServicePlan is not correct. An Account does not own a
service plan, it just retains a reference to the service plan a user
chose, which can be changed at any time the user wishes.
I thought perhaps ServicePlans “has_many” Accounts, but that doesn’t
seem right either. In working with an account, I should be able to
access it’s options by saying MyAccount.service_plan.someOption
(shouldn’t I?) because the service plan that was chosen is a property
of the Account. Also, HABTM doesn’t seem quite right because it’s
not a many-to-many relationship, it’s many-to-one.
Anyway, I’m just running in circles here and getting more confused.
Can anyone point me in the right direction? I’d be really grateful.