1

I have the following association in my Rails app:

Order has_and_belongs_to_many Type
Type has_and_belongs_to_many Order

There is a join table called OrdersTypes.

I know I can do Order.create(:types => [Type.first]).

However, if I am designing an API and I want the User to be able to create an order with just a Type id, how would the parameters look?

Something like Order.create(:type_ids => [1,2,3,4])

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • possible duplicate of [How to create an object for a Django model with a many to many field?](http://stackoverflow.com/questions/6996176/how-to-create-an-object-for-a-django-model-with-a-many-to-many-field) – Dave Satch May 07 '14 at 02:18
  • Do you mean create an order with single Type id ? – Asim Mushtaq May 07 '14 at 06:24

1 Answers1

0

You would do

Order.create(:type_id => params['id'].map(&:to_i) )

where params['id'] are the ids taken as input from user

Asim Mushtaq
  • 445
  • 5
  • 14