Michal
Michal

Reputation: 139

Rails 4 - form object values accessed in the find_by_sql query

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.

Upvotes: 0

Views: 156

Answers (2)

evanbikes
evanbikes

Reputation: 4171

Could you not just do:

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

Upvotes: 0

Michal
Michal

Reputation: 139

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

Upvotes: 0

Related Questions