create a model instance with associations

stacker

I'm working on a Rails app. I encountred a problem with Rails associations: I got 3 models: User, Company and CompanyMember

#  id :integer not null, primary key
class User < ActiveRecord::Base
  has_one :company_member
  has_one :company, through: :company_member
end

#  id          :integer          not null, primary key
#  name        :string(255)      not null
class Company < ActiveRecord::Base
  has_many :company_members
  has_many :members, through: :company_members, foreign_key: "user_id", source: :user
  has_many :admins, -> {where admin: true }, through: :company_members, foreign_key: "user_id", source: :user
end

#  id         :integer          not null, primary key
#  company_id :integer
#  user_id    :integer
#  admin      :boolean
#  created_at :datetime
#  updated_at :datetime
class CompanyMember < ActiveRecord::Base
  belongs_to :company
  belongs_to :user
end

An user can have only one company. He can be admin of this company. A Company can have more than one admin.

so when i want to add a member to the company i do : company.members << user. it's working: when i query company.members it returns the user and the the company_member record is like {user_id: 5, company_id: 8, admin: false}

Now i want to add an user as admin: what i have to do would be this: company.admins << user then company.members should return the user same as company.admins and the company_member record should be like {user_id: 5, company_id: 8, admin: true}

It is possible through Rails associations ? Because actually, it's not working :/ : the company_member is recorded as {user_id: 5, company_id: 8, admin: false} Can you help me ?

Thanks !

I'm using Rails 4.1.3 and ruby 2.1.2

stacker

I thought what if anyone is interested:

In Company, the association admins must be like this:

class Company < ActiveRecord::Base
  has_many :company_members
  has_many :members, through: :company_members, source: :user
  has_many :admins, -> {where(company_members: {admin: true })}, through: :company_members, source: :user
end

Now I have the expected behavior when i do

company.admins << user

It adds instanciate a company_member with admin: true (which defaults to false)

so I don't need the suggested callback from SampritiPanda but thanks dude !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

create a model instance with associations

From Dev

Factory Girl - How to create a factory for a model that has associations?

From Dev

Order processing in Rails - what is the best way to model this and create the associations?

From Dev

Create instance of model with field with choices

From Dev

Create instance of model from method

From Dev

Rails run after create on parent model after saving parent model AND nested associations

From Dev

Array of Associations for a Model

From Dev

Trouble with model associations

From Dev

Where clause on model associations

From Dev

CakePHP containables and model associations

From Dev

Model associations for CakePHP

From Dev

FactoryGirl Model Spec with Associations

From Dev

Securely building model associations

From Dev

About Rails Model Associations

From Dev

How to create a new instance of a model in sailjs?

From Dev

Can't create new instance of django model

From Dev

How create a new instance of a Model that is registered with a Controller?

From Dev

Automatically create instance of a model every day with rails

From Dev

How to create a model instance that has a belongsTo property?

From Dev

php create instance of loaded model in controller

From Dev

ModelForm won't create instance of model

From Dev

Updating a HABTM model associations only deletes the associations

From Dev

Updating a HABTM model associations only deletes the associations

From Dev

Create a field in form for each model instance related to another instance in django

From Dev

Rails - Create instance of a model from within another model

From Dev

How to create a model in Django based on the instance of another model, but filtered

From Dev

Autofill Django model fields when create instance of another model

From Dev

Sequelize create object with associations

From Dev

Speeding up associations in model specs with FactoryGirl - create vs build vs build_stubbed

Related Related

  1. 1

    create a model instance with associations

  2. 2

    Factory Girl - How to create a factory for a model that has associations?

  3. 3

    Order processing in Rails - what is the best way to model this and create the associations?

  4. 4

    Create instance of model with field with choices

  5. 5

    Create instance of model from method

  6. 6

    Rails run after create on parent model after saving parent model AND nested associations

  7. 7

    Array of Associations for a Model

  8. 8

    Trouble with model associations

  9. 9

    Where clause on model associations

  10. 10

    CakePHP containables and model associations

  11. 11

    Model associations for CakePHP

  12. 12

    FactoryGirl Model Spec with Associations

  13. 13

    Securely building model associations

  14. 14

    About Rails Model Associations

  15. 15

    How to create a new instance of a model in sailjs?

  16. 16

    Can't create new instance of django model

  17. 17

    How create a new instance of a Model that is registered with a Controller?

  18. 18

    Automatically create instance of a model every day with rails

  19. 19

    How to create a model instance that has a belongsTo property?

  20. 20

    php create instance of loaded model in controller

  21. 21

    ModelForm won't create instance of model

  22. 22

    Updating a HABTM model associations only deletes the associations

  23. 23

    Updating a HABTM model associations only deletes the associations

  24. 24

    Create a field in form for each model instance related to another instance in django

  25. 25

    Rails - Create instance of a model from within another model

  26. 26

    How to create a model in Django based on the instance of another model, but filtered

  27. 27

    Autofill Django model fields when create instance of another model

  28. 28

    Sequelize create object with associations

  29. 29

    Speeding up associations in model specs with FactoryGirl - create vs build vs build_stubbed

HotTag

Archive