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