Accepts_Nested_Attributes_For With Belongs_To Polymorphic

accepts_nested_attributes_for with belongs_to polymorphic

Just figured out that rails does not supports this kind of behavior so I came up with the following workaround:

class Job <ActiveRecord::Base
belongs_to :client, :polymorphic=>:true, :autosave=>true
accepts_nested_attributes_for :client

def attributes=(attributes = {})
self.client_type = attributes[:client_type]
super
end

def client_attributes=(attributes)
self.client = type.constantize.find_or_initialize_by_id(attributes.delete(:client_id)) if client_type.valid?
end
end

This gives me to set up my form like this:

<%= f.select :client_type %>
<%= f.fields_for :client do |client|%>
<%= client.text_field :name %>
<% end %>

Not the exact solution but the idea is important.

accepts_nested_attributes_for with belongs_to polymorphic devise

My mistake, I'm use nested form

<%= f.fields_for :rolable do |rf| %>
....
<% end %>

change to

<%= f.fields_for :rolable_attributes do |rf| %>
....
<% end %>

Rails: belongs_to & accepts_nested_attributes_for, polymorphic

In order for a model to accept nested attributes for another model, the association to the other model needs to be declared. In Profile you have accepts_nested_attributes_for :students, but there is no corresponding association defined (e.g. has_many :students), which is why you're getting that particular error. In your case, this association wouldn't be correct, however.

Usually, if model A accepts nested attributes for model B, either A has_many B or A has_one B. In your case, you have A belongs_to B. A better design would be

class Profile < ActiveRecord::Base
belongs_to :identifiable, polymorphic: true

class Student < ActiveRecord::Base
attr_accessible :profile_attributes
has_one :profile, as: :identifiable
accepts_nested_attributes_for :profile

belongs_to with polymorphic relationship will not delete from a has_many relationship via nested form

The issue is that Recipe is related to RecipeStep in two ways:

  1. has_many :recipe_steps
  2. has_many :recipe_steps, as: :stepable via the stepable.rb concern

Having both of these be :recipe_steps was the issue. I changed the concern to:
has_many :through_steps, as: :stepable, class_name: 'RecipeStep' and this solved the deletion issue.

Polymorphic has_one built through accepts_nested_attributes_for is not setting polymorphic type

Not sure if this is the cause, but you left out the polymorphic part in the other side of the relation ( the has one side )

has_one :calculator,
inverse_of: :calculable,
foreign_key: :calculable_id,
dependent: :destroy,
as: :calculable # <== this part

How to save a belongs_to association for a polymorphic model with nested attributes?

In case with belongs_to you will have something like: company related to address (through main_address_id) which in return is related to another(Company or Person) through polymorphic addressable.

As example you could change:

has_one :address, as: :addressable

to:

has_many :address, as: :addressable

and then add to your Address:

enum address_type: [:primary, :secondary]

Creating new resources with nested form with polymorphic relationships in Rails 4 presence

I created a fresh similar project. And I also got the same error. In my attempt to fix this problem, I stumbled upon inverse_of, and it now worked.

Now try

class DealerUser < User
# ..
belongs_to :dealer, polymorphic: true, foreign_key: :loginable_id, foreign_type: :loginable_type, inverse_of: :dealer_user
# ..
end

class Dealer < ActiveRecord::Base
# ..
has_one :dealer_user, as: :loginable, dependent: :destroy, inverse_of: :dealer
# ..
end


Related Topics



Leave a reply



Submit