Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord is not updating ID for polymorphic association accessed by :through after saving object
    primarykey
    data
    text
    <p>I'm trying to create a set of models in which user's profile has one address assigned but user can have many addresses in his virtual "address book". The user should be able to pick up the default address from all his addresses and assign it to profile as home_address.</p> <p>The addresses can have many types which differs. There are home addresses but there are also company and bank addresses that have additional fields. That's why I have to use polymorphism.</p> <p>I created the models:</p> <pre><code>class Addresser &lt; ActiveRecord::Base belongs_to :addressable, :polymorphic =&gt; true belongs_to :address, :polymorphic =&gt; true attr_accessible :address_id, :address_type end class HomeAddress &lt; ActiveRecord::Base has_one :addresser, :as =&gt; :address, :inverse_of =&gt; :address attr_accessible :name, :city, :zip, :street, :state, :country end class Profile &lt; ActiveRecord::Base has_one :home_address, :through =&gt; :addresser, :source =&gt; :address, :source_type =&gt; 'HomeAddress', :autosave =&gt; true has_one :addresser, :as =&gt; :addressable, :inverse_of =&gt; :addressable, :autosave =&gt; true attr_accessible :name, :gender end </code></pre> <p>Everything is working almost fine but Rails is not updating one of the fields after saving the models:</p> <pre><code>profile_data = { :name =&gt; 'Some User', :gender =&gt; 'm' } address_data = { :street =&gt; 'Streeting 7', :country =&gt; 'PL', :city =&gt; 'New Rails', } my_profile = Profile.new(profile_data) =&gt; #&lt;Profile id: nil, name: "Some User", gender: "m", created_at: nil, updated_at: nil&gt; my_address = HomeAddress.new(address_data) =&gt; #&lt;HomeAddress id: nil, street: "Streeting 7", city: "New Rails", country: "PL my_profile.home_address = my_address INSERT INTO `addressers` (`address_id`, `address_type`, `addressable_id`, `addressable_type`) VALUES (NULL, 'HomeAddress', NULL, 'Profile') COMMIT =&gt; #&lt;HomeAddress id: nil, name: "Sir Adam Rails", street: "Streeting 7", city: "New Rails", country: "PL"&gt; my_profile.save! INSERT INTO `profiles` (`created_at`, `name`, `gender`, `updated_at`) VALUES ('2012-06-23 10:07:51', 'Some User', 'm', '2012-06-23 10:07:51') INSERT INTO `home_addresses` (`city`, `country`,`street`) VALUES ('New Rails', 'PL', 'Streeting 7') UPDATE `addressers` SET `addressable_id` = 1 WHERE `addressers`.`id` = 1 COMMIT =&gt; true </code></pre> <p>In the above the addressable_id is updated but address_id is not. That causes the constraint to be invalid and unusable after reopenning database session:</p> <pre><code>Addresser.first SELECT `addressers`.* FROM `addressers` LIMIT 1 =&gt; #&lt;Addresser id: 1, address_id: nil, address_type: "HomeAddress", addressable_id: 1, addressable_type: "Profile"&gt; </code></pre> <p>Since address_id is nil, the profile.home_address cannot be found.</p> <p>Can I fix this without manually updating the address_id of addressable?</p> <p>The architecture of my app requires me to build the whole object with some initial settings and do the atomic save, so building address and saving it before user (which owns the profile) is created is a weak option.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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