Rails 4 - form object values accessed in the find_by_sql query

157 views Asked by At

In rails 4 I try to access some of the form object values in the find_by_sql. Is it possible?

Part of the code (in the Active Admin class):

f.has_many :actions, allow_destroy: false, new_record: true do |ff|
     ff.input :description,             
            label: ScenarioStep.where('id=?', ff.object.scenario_step_id).pluck(:name).pop.to_s, 
            hint: Action.find_by_sql("
                     select description from actions where scenario_step_id = #{ff.object.scenario_step_id}").to_s,        
            placeholder: 'test'
     ff.input :action_status, :as => :radio, :collection => Action.statuses
end

Value of the ff.object.scenario_step_id is accessible when I substitute query in the find_by_sql by its ActiveRecord equivalent.

While used in the find_by_sql, I get an error.

2

There are 2 answers

0
Michal On

This one was helful: Rails find_by_sql using ruby variable

Here is the solution that works for me:

hint: Action.find_by_sql(["
     select description from actions where scenario_step_id = ?", ff.object.scenario_step_id]).to_s
0
evanbikes On

Could you not just do:

hint: ff.object.actions.pluck(:description)