How to build object with many relationship

user1670773

I have 3 models User, Course and Group. Their relationship is as follows

user.rb

has_and_belongs_to_many :courses
  belongs_to :group

group.rb

has_many :users
  belongs_to :course

course.rb

has_many :groups,  dependent: :destroy
  has_and_belongs_to_many :users

I want to create a new group. How can I build group object? My user and course objects are below

   def new
    @user = User.find(current_user.id)
    @course = Course.find(params[:course])
    @group = need to build object here
  end

Previously I used to do something like this

@group = @user.groups.build

But now I also have @course object. How can do this?

Arup Rakshit

Here is what you can do:

 def new
    @user = User.find(current_user.id)
    @course = Course.find(params[:course])
    @group = @user.build_group(course: @course)
    # you can do @user.create_group!(course: @course) to create
    # the object in-place instead of in memory.
 end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to build a Create View for a Many to Many relationship

From Dev

How to create an object in a one to many relationship

From Dev

How to create an object in a one to many relationship

From Dev

How to fetch array to object from many-to-many relationship

From Dev

How to create a one-to-many relationship with JDBI SQL object API?

From Dev

how to flatten many to many relationship

From Dev

how to query a many to many relationship?

From Dev

How to save many to many relationship?

From Dev

how to flatten many to many relationship

From Dev

Sequelize Querying Many to Many Relationship and Accessing Object

From Dev

Delete object from an entity with many to many relationship

From Dev

Simple approach for Adding object in a many to many relationship

From Dev

Sequelize.js Node.js: How to Pass Already Created Object to Create Many-to-Many relationship?

From Dev

How I can delete grails domain object with many-to-many relationship?

From Dev

Sequelize.js Node.js: How to Pass Already Created Object to Create Many-to-Many relationship?

From Dev

Build vs Create in has many through relationship

From Dev

How to build has_many :through relationship between the same Model (User) in Rails?

From Dev

Build bidimensional table based on many-to-many EF relationship

From Dev

How to set strong parameter for many to many relationship

From Dev

How to get many to many relationship items in laravel

From Dev

How to save tag with many to many relationship in ActiveRecord

From Dev

How many to many relationship in Entity Framework is working

From Dev

How to write activemodel serializer for many to many relationship?

From Dev

SQLAlchemy. How to order on many to many relationship?

From Dev

how to save Many to Many relationship in django

From Dev

How to use checkboxes in a many-to-many relationship?

From Dev

jpa - How to create a many to many relationship with an IdClass?

From Dev

how to define many to many relationship in laravel models?

From Dev

How to insert data in many to many relationship

Related Related

  1. 1

    How to build a Create View for a Many to Many relationship

  2. 2

    How to create an object in a one to many relationship

  3. 3

    How to create an object in a one to many relationship

  4. 4

    How to fetch array to object from many-to-many relationship

  5. 5

    How to create a one-to-many relationship with JDBI SQL object API?

  6. 6

    how to flatten many to many relationship

  7. 7

    how to query a many to many relationship?

  8. 8

    How to save many to many relationship?

  9. 9

    how to flatten many to many relationship

  10. 10

    Sequelize Querying Many to Many Relationship and Accessing Object

  11. 11

    Delete object from an entity with many to many relationship

  12. 12

    Simple approach for Adding object in a many to many relationship

  13. 13

    Sequelize.js Node.js: How to Pass Already Created Object to Create Many-to-Many relationship?

  14. 14

    How I can delete grails domain object with many-to-many relationship?

  15. 15

    Sequelize.js Node.js: How to Pass Already Created Object to Create Many-to-Many relationship?

  16. 16

    Build vs Create in has many through relationship

  17. 17

    How to build has_many :through relationship between the same Model (User) in Rails?

  18. 18

    Build bidimensional table based on many-to-many EF relationship

  19. 19

    How to set strong parameter for many to many relationship

  20. 20

    How to get many to many relationship items in laravel

  21. 21

    How to save tag with many to many relationship in ActiveRecord

  22. 22

    How many to many relationship in Entity Framework is working

  23. 23

    How to write activemodel serializer for many to many relationship?

  24. 24

    SQLAlchemy. How to order on many to many relationship?

  25. 25

    how to save Many to Many relationship in django

  26. 26

    How to use checkboxes in a many-to-many relationship?

  27. 27

    jpa - How to create a many to many relationship with an IdClass?

  28. 28

    how to define many to many relationship in laravel models?

  29. 29

    How to insert data in many to many relationship

HotTag

Archive