mercredi 14 octobre 2015

rails how to combine has_one through and has_many through?

I've got users and organizations. Users should be able to become owners of 1 organization. So each organization can have multiple owners, but a user can have 0 or 1 organizations. So i made the following models:

class user < ActiveRecord::Base
  has_one :owner
  has_one :organization, through: :owners
end

class Owner < ActiveRecord::Base
  belongs_to :user
  belongs_to :organization
end

class Organization < ActiveRecord::Base
  has_many :owners
  has_many :users, through: :organizations
end

Is this even possible? All the examples seem to work with 2x has_one or 2x Has_many but I've never seen them combined. If this is possible I would like to know how to create an easy database record. I tried:

@organization = user.organization.build(organization_params)

@organization = current_user.build_organization(organization_params)

But this doesn't work.

Aucun commentaire:

Enregistrer un commentaire