Forum: Ruby on Rails has_one vs. has_many - Something just isn't seeming right...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E61b179b61fcd0441fa74a99a26ed472?d=identicon&s=25 Cliff Pruitt (Guest)
on 2007-07-09 16:28
(Received via mailing list)
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.

- Cliff
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2007-07-09 17:19
(Received via mailing list)
I think you want something like this

class User < ActiveRecord::Base
  belongs_to :account

# an account can have many users
class Account < ActiveRecord::Base
  belongs_to :service_plan
  has_many :users

# each account belongs to a service plan, so all users in the account
# will have the same service plan
class ServicePlan < ActiveRecord::Base
  has_many :accounts

E61b179b61fcd0441fa74a99a26ed472?d=identicon&s=25 Cliff Pruitt (Guest)
on 2007-07-09 20:20
(Received via mailing list)
On Jul 9, 2007, at 11:18 AM, Mike Garey wrote:

> end
Thanks Mike, That's what I started to think, but it didn't seem quite
correct from a data modeling standpoint.  Maybe it's just adapting to
the way rails does things & I just need to adjust.  It doesn't seem
like a service plan should really "own" anything at all, but I
understand now how it technically works within Rails.  I had tried
exactly what you'd recommended before but another (very stupid in
hindsight) mistake in my controller kept it from having any effect or
generating any error, so your email was enough to push me back into
looking a little harder.  It's now working. Thanks a lot.

- Cliff
This topic is locked and can not be replied to.