Rails admin has_many :through relationship causing

2019-09-07 14:14发布

I have a rails app with a has_many :through model like so:

class Blog < ActiveRecord::Base
  has_many :blog_categorizations, dependent: :destroy
  has_many :categories, :through => :blog_categorizations
  accepts_nested_attributes_for :categories, allow_destroy: true
end

class Category < ActiveRecord::Base
  has_many :blog_categorizations, dependent: :destroy
  has_many :blog, :through => :blog_categorizations
  accepts_nested_attributes_for :blog, allow_destroy: true
end

class BlogCategorization < ActiveRecord::Base
  belongs_to :blog
  belongs_to :category
end

In my Rails admin configuration I have:

config.model Blog do
    edit do
      field :title
      field :content
      field :source
      field :author
      field :date
      field :photos
      field :categories
    end
  end

Now when I attempt to add a new Blog through rails_admin, or attempt to edit a pre-existing one, I get infinite rendering of rails_admin templates like this:

Started GET "/admin/recipe/new?_pjax=%5Bdata-pjax-container%5D" for 127.0.0.1 at 2014-10-29 07:41:35 -0700
  ActiveRecord::SchemaMigration Load (0.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by RailsAdmin::MainController#new as HTML
  Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"recipe"}
  User Load (1.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (2.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (1.0ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (1.7ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (25.6ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (16.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_submit_buttons.html.haml (2.9ms)
   (0.6ms)  SELECT COUNT(*) FROM "blogs"
  Recipe Load (0.7ms)  SELECT "blogs".* FROM "blogs"   ORDER BY blogs.id desc
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_select.html.haml (14.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_file_upload.html.haml (4.0ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (1.6ms)
   (0.4ms)  SELECT COUNT(*) FROM "blog_categorizations"
  BlogCategorization Load (0.3ms)  SELECT "blog_categorizations".* FROM "blog_categorizations"   ORDER BY blog_categorizations.id desc
  Category Load (0.5ms)  SELECT "categories".* FROM "categories"  WHERE "categories"."id" IN (1, 5)
  Recipe Load (0.3ms)  SELECT "blogs".* FROM "blogs"  WHERE "blogs"."id" IN (65)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_multiselect.html.haml (22.1ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.6ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (0.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (0.9ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)
  CACHE (0.0ms)  SELECT "blogs".* FROM "blogs"   ORDER BY blogs.id desc
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_select.html.haml (4.0ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_file_upload.html.haml (0.5ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  CACHE (0.0ms)  SELECT "blog_categorizations".* FROM "blog_categorizations"   ORDER BY blog_categorizations.id desc
  CACHE (0.0ms)  SELECT "categories".* FROM "categories"  WHERE "categories"."id" IN (1, 5)
  CACHE (0.0ms)  SELECT "blogs".* FROM "blogs"  WHERE "blogs"."id" IN (65)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_multiselect.html.haml (3.9ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.1ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (0.3ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (1.0ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)
  Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)

This same output just occurs infinitely and doesn't seem to stop, almost as if it's entered an infinite loop of loading the blogs categories, then the categories blogs, etc.

How can I stop this from occuring?

1条回答
不美不萌又怎样
2楼-- · 2019-09-07 14:29

Removing accepts_nested_attributes_for :blog in Category.rb fixed the problem

accepts_nested_attributes_for :blog

This was previously in Category.rb but removed in the code posted in question.

Also, leaving

accepts_nested_attributes_for :blog

but adding inverse_of: category for blog model like

has_many :blog, :through => :blog_categorizations, inverse_of: category

and

has_many :blog, :through => :blog_categorizations, inverse_of: blog

caused the problem to go away. I'm currently using the inverse_of option even though it isn't supposed to work for :through models, but for some reason works for Rails Admin in many-to-many.

查看更多
登录 后发表回答