Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_many :through -- Adding metadata to the through relationship
    primarykey
    data
    text
    <p>I have a need to add metadata about a HABTM relationship. I wanted to use a has_many :through relationship to accomplish this, but it is not necessary. Here is the problem simplified:</p> <pre><code>class Customer &lt; ActiveRecord::Base has_many :customer_teddy_bears has_many :teddy_bears, :through =&gt; :customer_teddy_bears end class CustomerTeddyBear &lt; ActiveRecrod::Base belongs_to :customer belongs_to :teddy_bear attr_accesible :most_favoritest # just to show it exists, boolean end class TeddyBear &lt; ActiveRecord::Base has_many :cusomter_teddy_bears end </code></pre> <p>So what I need to do is start adding teddy bears to my customers, Teddy Bears are a fixed set of data, lets say a fireman_bear, doctor_bear, dominatrix_bear. Any customer can claim to own a kind of teddy bear, but they also specify which is their most favoritest bear. Since I cannot modify the bears model because that is globally shared among all customers I am adding the metadata (among other metadata) to CustomerTeddyBear.</p> <p>The problem is that the following does not work.</p> <pre><code>customer = Customer.new # new record, not yet saved, this must be handled. customer.teddy_bears &lt;&lt; fireman_bear customer.teddy_bears &lt;&lt; doctor_bear # now to set some metadata favoritest_record = customer.customer_teddy_bears.select{|ctb| ctb.teddy_bear == doctor_bear}.first favoritest_record.most_favoritest = true </code></pre> <p>The above code does not work since customer_teddy_bears entries are only populated during save when creating records in the database. Is there another mechanism for doing this?</p> <p>If there is nothing "automated" built into rails I will just have to manually manage this relationship by including <code>teddy_bears</code> when I select <code>customer_teddy_bears</code> and using techniques like </p> <pre><code>def teddy_bears self.customer_teddy_bears.map(&amp;:teddy_bear) end </code></pre> <p>along with manually creating the associations, and not using a :through relationship.</p> <p>please note, all this must happen before the <code>#save</code> is executed on the <code>Customer</code> object, so I need to set all relevant metadata while still in-memory.</p> <p>Recommendations I got from #RubyOnRails</p> <pre><code>ctb = customer.customer_teddy_bears.build({:customer =&gt; customer, :teddy_bear =&gt; fireman_bear}) ctb2 = customer.customer_teddy_bears.build({:customer =&gt; customer, :teddy_bear =&gt; doctor_bear}) ... ctb.most_favoritest = true </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload