Has_one through belongs_to

Hi.

My model data is the following:

  • Place
  • User
  • Checkin

Instead of having a Checkin that belongs to both User and Place, I chose
to
add another model, Program, that allows to list all the places a user
can
checkin into, and also hide the checkins he’s no longer involved in (=
the
user sign out of a specific program).
That give me crazy relationships:
Program has_many Checkin (and a double belongs_to Place and User)
Checkin belongs_to Program
User has_many Program
Place has_many Program
Place has_many Checkin through Program

In order to access a place or a user from a checkin, I do a has_one
through
(program): Checkin has_one User through Program. It allows me to have
clean
ActiveRecord relationships.
But I don’t know if it is the most suitable, and if has_one is made for
this
(apparently not really).

Michel ( Dagnan ) wrote in post #1009516:

In order to access a place or a user from a checkin, I do a has_one
through
(program): Checkin has_one User through Program. It allows me to have
clean
ActiveRecord relationships.
But I don’t know if it is the most suitable, and if has_one is made for
this
(apparently not really).

So if I recall correctly (no Rails work for many moons):

user
:has_many :programs

place
:has_many :programs

program
:belongs_to :user
:belongs_to :place
:has_one :checkin

checkin
:belongs_to :program

then

checkin.program.user should get you that checkin’s user, and similarly
checkin.program.place should get you that checkin’s place

has_many/one and belongs_to imply hierarchy. Your checkin is the low
man on the totem pole, and doesn’t have anything… it belongs to, but
you can traverse the parents easily.