Note that there are some explanatory texts on larger screens.

plurals
  1. PONested model not available in Devise views
    primarykey
    data
    text
    <p>I'm having a problem with associations not being available in my views.</p> <p>My models are:</p> <pre><code>:user has_many :subscriptions :subscription belongs_to :user </code></pre> <p>I'm using Devise to manage authentication etc. for Users</p> <p>What I'd like to do: when creating a new user in the registration process, I also want to also create a subscription for that user. Since <code>Devise::RegistrationsController#new</code> by default does not initialize an associated subscription, I've created my own <code>RegistrationsController</code>:</p> <pre><code>class RegistrationsController &lt; Devise::RegistrationsController def new super resource.subscriptions.build logger.debug resource.subscriptions.inspect end end </code></pre> <p>The debug statement there confirms that a <code>Subscription</code> object is successfully created:</p> <pre><code>[#&lt;Subscription id: nil, user_id: nil, chargify_subscription_id: nil, chargify_product_handle: nil, created_at: nil, updated_at: nil&gt;] </code></pre> <p>The problem: in the view, <code>resource.subscriptions</code> does not exist. If I inspect <code>resource</code> in the view, I get a <code>User</code> object that includes all of its own attributes but no associations (it should have an associated <code>subscriptions</code>)</p> <p><code>debug(resource)</code> gives the following:</p> <pre><code>--- !ruby/object:User attributes: name: encrypted_password: "" created_at: updated_at: last_sign_in_ip: last_sign_in_at: sign_in_count: 0 last_name: current_sign_in_ip: reset_password_token: current_sign_in_at: remember_created_at: reset_password_sent_at: chargify_customer_reference: first_name: email: "" attributes_cache: {} changed_attributes: {} destroyed: false marked_for_destruction: false new_record: true previously_changed: {} readonly: false </code></pre> <p>Is there something I'm missing, or perhaps is there something strange about the <code>resource</code> mechanism used by Devise that prevents associations from being available in the view?</p> <p>Thanks!</p> <p>Edit: If I just add <code>resource.subscriptions.build</code> in my view before rending the form, that works fine. But I think that kind of logic belongs in the controller and not the view, and I'd like to know what's keeping me from being able to put it there.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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