Dennis
Dennis

Reputation: 3

Rails Join Table without Model

Is it possible to create a join table without a model. I have a model called User and Pet. I want user to have only one pet and pet to only have one owner. Very simple. However, I am trying to figure out how to create a join table

pets_users that has both users.id and pets.id in it without having to create the actual model. Is this possible? Is this a bad design?

If possible, how do I create a new entry in the pets_users table?

Upvotes: 0

Views: 816

Answers (1)

Toby Hede
Toby Hede

Reputation: 37133

You don't need a join table for this.

User has_one Pet
Pet belongs_to User

The pet table then would have a user_id column and Active Record handles the rest for you.

Upvotes: 1

Related Questions