Validating presence of parent in polymorphic association with nested attributes #1629

Closed
6twenty opened this Issue Jun 10, 2011 · 10 comments

Projects

None yet

9 participants

@6twenty

Taking the following classes:

class User < ActiveRecord::Base
  has_one :address, :as => :addressable
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  belongs_to :addressable, :polymorphic => true
  validates_presence_of :addressable
end

Now when creating a new User (with fields_for :address), no records will be saved as the validation on Address will fail with address.addressable can't be blank.

On a non-polymorphic relationship, the solution to this is to use :inverse_of in the User model, eg: has_one :address, :inverse_of => :user. However :inverse_of does not currently support polymorphic relationships, so this is not an option.

@6twenty

This was tested with Rails 3.0.7.

@tom-kuca

As far as I know, there is currently no way to get it working without some manual work.See #1383

@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@ddemaree ddemaree Fixed bug where calling app method from console would raise ArgumentE…
…rror [#1629 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
f90160c
@isaacsanders

Is this still an issue @6twenty?

@6twenty

Not sure, but I'll do a quick test when I have some free time.

@murdoch

I'm fairly sure this is still an issue, running on rails 3.2.6. Are there any workarounds?

@gylaz

Have you tried?

validates :addressable, presence: true, associated: true

or adding:

validates_associated :addressable

@jasonayre

2nd method, validates_associated :attribute seems to be working for me

@Altonymous

This still doesn't seem to work for me when creating new records. If I want to make sure that the parent association is in place it fails due to the parent_id being nil. {poly}_type seems to work fine on validation.

@brycesenz

@Altonymous - Have you tried the :inverse_of method for your nested models? See this post for examples -

https://rails.lighthouseapp.com/projects/8994/tickets/2815-nested-models-build-should-directly-assign-the-parent

@steveklabnik
Ruby on Rails member

I believe this is expected behavior.

I made a test case with @kcurtin here: https://gist.github.com/steveklabnik/5392274

Basically, if you use validates_presence_of, you need the parent to be saved before you get made. In this case, the Address isn't able to find a User because the User hasn't been created yet, we're trying to make them both at the same time. If we only cared that the Users are valid, we can use validates_associated, but if we need it to always be there we have to make it first, and AR won't automate this process.

@dLobatog dLobatog referenced this issue in theforeman/foreman Oct 8, 2015
Closed

fixes #12097 - compatible updates in user_test #2795

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment