has_one vs. Belongs_to??

I have clients, (table - model-controller) and I have plans (table - model - controller). A client can have several plans and a plan can only map to one client. I would assume that in the model for clients i need a has_many and in the model for plans I need either a has_one or a belongs_to. I am not sure which to use and obviousely it is not entirely clear to me (after reading in agile web developement) what these methods are doing for me, other than it seems to be obviouse i should have these relationships in place.

belongs_to would look like this:

clients (table) id name etc

plans (table) id client_id name

client.rb (model) has_many :plans

plan.rb (model) belongs_to :client

has_one is just like has_many, but it restricts the selection.

clients (table) id name plan_id etc

plans (table) id name

client.rb (model) belongs_to :plan

plan.rb (model) has_one :client

The first example is probably what you want (one to many) and the second example is probably not what you want (one-to-one).

belongs_to is used on the model that contains the foreign key. If the plans table has the client_id foreign key, the plan belongs_to :client and the client has_many :plans. (see how that makes sense when you say it out loud?) It doesn’t make as much sense to say that a client belongs_to a plan though.

Hopefully I helped clear it up a bit instead of confusing you more.

thank you both, super helpful JE