Search code examples
ruby-on-railsactiverecordrails-activerecordactiveadminactive-record-query

Activeadmin batch action caching query result


I am using a batch action in activeadmin to display a form and code is

batch_action 'Assign something', form: {
      abc: Abc.order('name ASC').map{|s| [s.name, s.id]}.uniq
  } do |ids, inputs|
    ids.each do |id|
      job = Job.find(id)
      # does something here
    end
    redirect_to :back, notice: "Congrats!!"
  end

This code generates perfect form but in this form dropdown generated has cached values. That is if we modify name of any Abc record, its change is not reflected in the form even after refreshing the page.

Form has following options

<option value="6">Bla</option>
<option value="7">Alliance</option>

After editing value Bla to Foo in DB our form still shows same old result. Though value in db has been changed.

After editing records. Though Bla should be changed to Foo now
<option value="6">Bla</option>
<option value="7">Alliance</option>

Any idea where I am going wrong?


Solution

  • According to this doc, dynamic forms need a proc.

    batch_action ('Assign something', form: ->{ {
      abc: Abc.order('name ASC').map{|s| [s.name, s.id]}.uniq
    } }) do |ids, inputs|
    ids.each do |id|
      job = Job.find(id)
      # does something here
    end
    redirect_to :back, notice: "Congrats!!"
    

    end

    I think the block passed as an argument and the block after the function call are less confusing (slightly) with the () around the arguments. They are not required.