1
votes

I have two models:

Model: Stores => Relavant fields: id, retailer_id
Model: Retailer => Relavant fields: name, id

The form I am trying to create is an activeadmin form which looks at the retailer name, then returns a list of stores that matches the name in check_boxes. What I want is the user the select the stores he wants associated with the retailer, and hit update, which will basically update the retailer_id in the stores model with the current retailer record.

If creating a new retailer, I want the checkboxes to show all stores where the retailer_id is blank.

In my retailer model, I have added

has_many :stores
accepts_nested_attributes_for :stores

in my stores model, I have added

belongs_to :retailer

here is what I currently have in my retailer activeadmin form

ActiveAdmin.register Retailer do
      action_item do
      link_to "View unassigned stores", "/admin/unassigned_stores"
    end 
  form :html => { :enctype => "multipart/form-data" } do |f|  
    f.inputs do
      f.input :name
      f.input :description, :as => :text
      f.input :photo, :label => "Retailer Logo", :as => :file, :hint =>     image_tag(retailer.photo.url)       
  f.input :retailer_id, :for => :Stores,  :as => :check_boxes, :collection => Store.find(:all, :conditions => ["retailer_id is null and (name like ? or name_2 like ?) ", "%#{retailer.name}%", "%#{retailer.name}%"]), :label => "Assign Stores to retailer"
   end 
f.buttons

end end

1
just an additional comment about this. my current code fails because it tries to update retailer_id in the retailer model which does not have the field in it. - RickJames79
Also, would an easier way to handle something like this be to set up a relations table with the two ids? and updating that instead? - RickJames79

1 Answers

1
votes

after a lot of hours searching around, came accross this little gem. ActiveAdmin -- Show list of checkboxes for nested form instead of a form to add items

basically, i changed :retailer_id to :stores and added, :store_ids to attr:accessible and that allowed me to do what i was trying to do.