ruby on rails - Rails_admin, change values of enum field depending of association -


i have 4 models: shop, item, category, subcategory

shops has has_and_belongs_to_many association category , subcategory.

shops has has_many association items.

category has has_many association subcategories.

both category , subcategory has_many items.

when create shop can choose lot of categories , subcategories.

when try create item, rails_admin creates select boxes categories , subcategories. however, there problem. it makes me choose categories , subcategories.

i want able select categories , subcategories belong selected shop.

is possible in rails_admin change select enums values depending on other models associations?

code category

class category     include mongoid::document     has_many :sub_categories, inverse_of: :category     has_many :items      accepts_nested_attributes_for :sub_categories end 

code subcategory

class subcategory     include mongoid::document     has_many :items     belongs_to :category, inverse_of: :sub_categories end 

code shop

class shop     include mongoid::document     has_many :items, dependent: :destroy, inverse_of: :shop     has_and_belongs_to_many :categories, inverse_of: nil     has_and_belongs_to_many :sub_categories, inverse_of: nil     accepts_nested_attributes_for :items end 

code item

class item     include mongoid::document     belongs_to :shop, inverse_of: :items     belongs_to :category     belongs_to :sub_category end 

============================ possible solution =======================

because there 1 sided has_and_belongs_to_many assocation between shop , category, means shop stores array of ids of categories.

also models mongoid documents, means can not use joins.

in item edit action have added code:

field :category     associated_collection_cache_all false     associated_collection_scope        item = bindings[:object]       shop = item.shop       proc.new { |scope|         scope = scope.where(id: {"$in" => shop.category_ids.map(&:to_s)}) if item.present?       }     end end 

now allows me choose categories shops categories. however, there problem can not use on create action

rails_admin supports scoped associations. see:

https://github.com/sferik/rails_admin/wiki/associations-scoping

for example:

config.model item   field :category     associated_collection_cache_all false       associated_collection_scope       item = bindings[:object]       proc.new { |scope|         scope = scope.joins(:shops).where(shops: {id: item.shop_id}) if item.present?       }     end   end end 

note warning "bindings[:object] can null new parent records!". may have save item before scope takes effect. in past i've added conditional active_admin form scoped field shows once record saved.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -