-2

I have the following models

class Order < ApplicationRecord
  has_many :order_details
  has_many :products, through: :order_details
end

class OrderDetail < ApplicationRecord
  belongs_to :order
  belongs_to :product
end

class Product < ApplicationRecord
  has_many :order_details
  has_many :orders, through: :order_details
end

And I already have product records in my database. Now, if using syntax: Order.create name: 'HH', product_ids: [1,2]

1 Order record is created, and rails automatically creates 2 more OrderDetail records to connect that Order record with 2 Products.

This syntax is quite handy.

Now, I want to learn more about it from the Rails documentation. But now i still can't find the documentation about it. Can someone help me find documents to learn more?

[Edit] Additional: I'd like to find documentation on the Rails syntax that allows passing a list of ids to automatically create records in the intermediate table, like the Order.create syntax with ```product_ids` `` that I gave above.

gaacode
  • 7
  • 2

1 Answers1

-1

The extensive documentation is at https://api.rubyonrails.org/, and many-to-many is here.

The essential part is to analyze the source code of Rails at Module (ActiveRecord::Associations::CollectionAssociation) and at id_writers method:

  # Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
  def ids_writer(ids)
    primary_key = reflection.association_primary_key
    pk_type = klass.type_for_attribute(primary_key)
    ids = Array(ids).compact_blank
    ids.map! { |i| pk_type.cast(i) }
    # .... code continues

We see that ids parameter (ex.: [1,2]) is first checked to be Array then the compact_blank method removes all falses values, after that, ids are casted to match primary_key type of the model (usually :id). Then code continues to query database with where to get found ids (associations) and saves.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
lestra
  • 312
  • 1
  • 7
  • Sorry, my question wasn't clear and could be misleading. I mean I'd like to find documentation on the Rails syntax that allows passing a list of ids to automatically create records in the intermediate table, like the ```Order.create``` syntax with ```product_ids``` that I gave above. Currently I still can't find information about that syntax type in the Rails documentation – gaacode Aug 12 '22 at 02:20
  • Well, sorry but I still don't get it completely. If you want to know more about .create syntax visit api link and search for create, like in [here](https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-create). But if you want to know about the specific many-to-many array as in product_ids, only way to find more about it, is research the source code like in [here](https://github.com/rails/rails/blob/04972d9b9ef60796dc8f0917817b5392d61fcf09/activerecord/lib/active_record/associations/collection_association.rb#L60) But no news, it's only an Array w numbers – lestra Aug 12 '22 at 02:40
  • if you look at the second reference in @lestra's response, you can see "Auto-generated methods", of which one is `other_ids = (id,id...)`. That is where the method is documented. – Les Nightingill Aug 12 '22 at 03:43