ActiveRecord::HasManyThroughNestedAssociationsAreReadonly Error in Rails Admin
Asked Answered
C

2

6

I just upgraded to Rails 3.2.10 and am getting an error message that I never used to get when updating a record via RailsAdmin.

ActiveRecord::HasManyThroughNestedAssociationsAreReadonly at /admin/vendor/12/edit

Message Cannot modify association 'Vendor#categories' because it goes through more than one other association.

This is my Vendor model:

class Vendor < ActiveRecord::Base
  attr_accessible :name, :description, :banner_image, :logo_image, :intro_text, :thumb_image, :category_ids, :product_ids, :user_id, :remove_banner_image, :banner_image_cache, :remove_logo_image, :logo_image_cache
    mount_uploader :banner_image, ImageUploader
    mount_uploader :logo_image, ImageUploader
    mount_uploader :thumb_image, ImageUploader

    has_many :products, :dependent => :destroy
    has_many :categories, :through => :products
    belongs_to :owner, :class_name => "User",
        :foreign_key => "user_id"   
end

This is my Category model:

class Category < ActiveRecord::Base
  attr_accessible :name, :product_ids, :category_ids
    has_many :category_products do
         def with_products
           includes(:product)
         end
       end

  has_many :products, :through => :category_products

end

This is my Product model:

class Product < ActiveRecord::Base
  attr_accessible :name, :description, :price, :vendor_id, :image, :category_ids, :sku, :remove_image, :image_cache
    mount_uploader :image, ImageUploader

    belongs_to :vendor
    has_many :category_products do
           def with_categories
             includes(:category)
           end
    end

    has_many :categories, :through => :category_products

end

This is my CategoryProduct model:

class CategoryProduct < ActiveRecord::Base
  attr_accessible :product_id, :category_id, :purchases_count

    belongs_to :product
  belongs_to :category

  validates_uniqueness_of :product_id, :scope => :category_id
end
Cubbyhole answered 6/1, 2013 at 22:0 Comment(0)
D
0

This happens because your association is nested, meaning (from rails source) : A through association is nested if there would be more than one join table... which is your case here.

Apparently a workaround (I didn't test) is telling Vendor it doesn’t need to autosave the association.

has_many :categories, :through => :products, :autosave => false
Davide answered 14/1, 2013 at 9:6 Comment(4)
What are the repercussions with this workaround? Will I not be able to update any categories from a vendors model?Cubbyhole
As i said : untested just pointing you in some direction. Give it a try and let me know... And if not, hopefully Rails Admin is flexible enough to call a save on an association...Davide
Just tried it, and unfortunately it doesn't work. Still the same issue :(Cubbyhole
@Cubbyhole late to the party, as always... Were you ever able to find a solution?Brothel
A
0

You can mark the association as readonly and rails_admin will then not generate the category fields in the form for vendor:

has_many :categories, -> { readonly }, through: :products

Acid answered 3/8, 2014 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.