title: Is it possible to store an attribute on a model, but is only accessible through the association?
body: <p>I'm building an ecommerce platform and was looking over the database design.</p>
  
  <p>These are the models in my ecommerce site:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    belongs_to :cart, {:optional =&gt; true}
  end
  
  
  class Cart &lt; ApplicationRecord
    has_and_belongs_to_many :phones
    has_one :user
  end
  
   class Phone &lt; ApplicationRecord
   has_and_belongs_to_many :carts # has join table with Cart
   has_and_belongs_to_many :orders
   end
  
  
  class Order &lt; ApplicationRecord
    belongs_to :user, {:optional =&gt; true}
    has_and_belongs_to_many :phones #HAS join table with Phone 
  end
  </code></pre>
  
  <p>My site is selling phones (<code>Model Phone</code>) to users (<code>Model User</code>), who put them in carts (<code>Model Cart</code>), and once the order gets submitted, an order gets created (<code>Model Order</code>). </p>
  
  <p>I want to update the quantities sold by putting <code>@user.cart.phones</code> (<code>@user</code> = current user) into the Order model, after the purchase has been submitted and finalized. The trouble I'm having is how I should design the database schema in so that I can track the type of phone being sold, and how many of that particular phone were sold in the order.</p>
  
  <p>I was thinking something along the lines of <code>@user.order.phones</code>, and have an attribute called quantity on the <code>Phone</code> model. The problem with that is that the Phone model would be accessible to all users, hence, it would overwritten the moment another user submitted their order.</p>
  
  <p>My question is, is it possible to have an attribute on the Phone model, only accessible to <code>@user.cart</code> (i.e.: <code>@user.cart.phones.phones_sold</code> --> <code>.phones_sold</code> would only be available through <code>@user.cart</code> and would be unique for each user)? If not, how could I go about designing my Model/Database to include an order model that has information about each order (type of phone, and quantity sold) that's unique to each individual user? </p>