Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested polymorphic #1338

Closed
Tuckie opened this issue Sep 27, 2012 · 15 comments
Closed

Nested polymorphic #1338

Tuckie opened this issue Sep 27, 2012 · 15 comments
Assignees
Milestone

Comments

@Tuckie
Copy link

Tuckie commented Sep 27, 2012

I'm attempting to have a nested polymorphic has_many with:

class Promotion < ActiveRecord::Base
  belongs_to :promotable, :polymorphic => true, :inverse_of => :promotions
  attr_accessible :promotable_id, :promotable_type 
end
class Page < ActiveRecord::Base
  has_many :promotions, :as => :promotable, :inverse_of => :promotable, :dependent => :destroy
  accepts_nested_attributes_for :promotions, :allow_destroy => true
  attr_accessible :promotions_attributes, :allow_destroy => true
end
class Offering < ActiveRecord::Base
  has_many :promotions, :as => :promotable, :inverse_of => :promotable, :dependent => :destroy
  accepts_nested_attributes_for :promotions, :allow_destroy => true
  attr_accessible :promotions_attributes, :allow_destroy => true
end

I would like to have a nested form within each page or offering, but I'm getting

undefined method `abstract_model' for #<Array:0x00000004c6e130>
...
/usr/local/rvm/gems/ruby-1.9.3-p0/bundler/gems/rails_admin-f20879f82ea0/app/helpers/rails_admin/form_builder.rb:47:in `field_wrapper_for'
/usr/local/rvm/gems/ruby-1.9.3-p0/bundler/gems/rails_admin-f20879f82ea0/app/helpers/rails_admin/form_builder.rb:37:in `block (2 levels) in fieldset_for'
/usr/local/rvm/gems/ruby-1.9.3-p0/bundler/gems/rails_admin-f20879f82ea0/app/helpers/rails_admin/form_builder.rb:37:in `map'
/usr/local/rvm/gems/ruby-1.9.3-p0/bundler/gems/rails_admin-f20879f82ea0/app/helpers/rails_admin/form_builder.rb:37:in `block in fieldset_for'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
haml (3.1.7) lib/haml/helpers/xss_mods.rb:109:in `with_output_buffer_with_haml_xss'
actionpack (3.2.3) lib/action_view/helpers/capture_helper.rb:40:in `capture'
haml (3.1.7) lib/haml/helpers/action_view_mods.rb:105:in `capture_with_haml'
actionpack (3.2.3) lib/action_view/helpers/tag_helper.rb:95:in `content_tag'
haml (3.1.7) lib/haml/helpers/action_view_mods.rb:123:in `content_tag_with_haml'

@bbenezech
Copy link
Collaborator

There is no support for nested polymorphics at the moment. I am not sure what it would involve. I'll investigate.

@ghost ghost assigned bbenezech Oct 2, 2012
@Tuckie
Copy link
Author

Tuckie commented Oct 2, 2012

Much thanks!

@brett-bender
Copy link

+1 This was working as of v0.0.5 and is a must-have feature for us. I was sad to see this error on upgrading my installed version.

@michaek
Copy link

michaek commented Oct 9, 2012

I've looked at this a little, and it seems like there's been a slight regression that's causing this error for me.

When nesting an associated model, :inverse_of is meant to hide the field that represents the current association (that the nested model will be saved into). That seems to not be the case for the polymorphic association that I'm testing with - when I exclude that field in the nested block, I no longer get the above error and my models persist correctly.

Hope that helps in tracking down the issue!

@billguy
Copy link

billguy commented Jan 11, 2013

@michaek is correct. This fixed my issue.

config.model Document do
    include_all_fields
    nested do
      field :documentable do
        visible false
      end
    end
  end

@chrisbloom7
Copy link

This setup worked for me.

class Note < ActiveRecord::Base
  belongs_to :notable, polymorphic: true # do not specify inverse_of, else rails_admin borks

  rails_admin do
    nested do
      field :text

      field :user do
        visible false
      end

      field :notable do
        visible false
      end

      field :user_id, :hidden do
        visible true
        help ""
        default_value do
          bindings[:view]._current_user.id
        end
      end
    end
  end
end


class Material < ActiveRecord::Base
  has_many :notes, :as => :notable, inverse_of: :notable
  accepts_nested_attributes_for :notes, 
    reject_if: proc { |attributes| attributes['text'].blank? },
    allow_destroy: true

  rails_admin do
    edit do
      group "Form" do
        field :notes
      end
    end
  end
end

@pencilcheck
Copy link
Contributor

What I see is that in nested_field_association method in file form_builder.rb
field.associated_model_config returns an array instead, so getting this error

undefined method `abstract_model' for #<Array:0x007fadec5b7da8>

This only happens if I add inverse_of to the children model. Using @chrisbloom7 example, it is the Note model.

Using 0.6.5

pencilcheck added a commit to pencilcheck/rails_admin that referenced this issue Nov 15, 2014
field.associated_model_config returns an array, but abstract_model is in field
@pencilcheck
Copy link
Contributor

Actually, if you use my fork

gem 'rails_admin', github: 'pencilcheck/rails_admin', branch: 'master'

In the time being, you can add inverse_of to both sides and it will work like a charm :)
Let's hope this is merged asap.

@victorbv
Copy link

victorbv commented Apr 1, 2015

@pencilcheck, thank you for your fork, it's working fine for me. I strongly recommend the merge.

@voyera
Copy link

voyera commented Jun 16, 2015

This fork is working fine for me as well. I'm not sure what the merge would entail but it would be worth investigating.

@vedant1811
Copy link

Hello, what is the status of this. Will this patch be merged, or we already have a better implementation?

@KonstantinKo
Copy link

+1
I need this fix as well

@davidwparker
Copy link

@bbenezech - @pencilcheck's fix worked for me as well. Do we expect this to be merged / supported soon?

peret pushed a commit to playtestcloud/rails_admin that referenced this issue Dec 2, 2015
Don't try to access abstract_model of Array.
@voyera
Copy link

voyera commented Dec 8, 2015

Hi guys, I created a pull request for that change but it seems like we need rspec tests for it. Anyone feels like tackling this? Unfortunately I'm not too familiar with that level of testing... and I would definitively learn from it!

bbenezech added a commit that referenced this issue Feb 4, 2016
@bbenezech
Copy link
Collaborator

Merged!

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

No branches or pull requests