I am creating a table of Employees and I would like to sort by full_rate_pence, which is an attribute of HourlyRate. Each employee has_many hourly_rates which form a history of pay increases or decreases. The problem is trying to access the nested HourlyRate because the HourlyRate I need to access is within a collection. It's not possible to use .find or .find_by because of the use of :includes
Initially I received the following error:
Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "hourly_rates.first.full_rate_pence asc".This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql().
Then once wrapping the part with Arel.sql() I received the next error:
SQLite3::SQLException: no such column: hourly_rates.full
Models
class Employee < ApplicationRecord
has_many :hourly_rates
accepts_nested_attributes_for :hourly_rates
end
class HourlyRate < ApplicationRecord
belongs_to :employee
monetize :full_rate_pence, as: 'full'
end
Link In Table
<th scope="col">
<%= sort_link(column: "hourly_rates.first.full", label: "Hourly Rate") %>
</th>
Helper Method to create link
def sort_link(column:, label:)
if column == params[:column]
link_to(label, list_employees_path(column: column, direction: next_direction))
else
link_to(label, list_employees_path(column: column, direction: 'asc'))
end
end
Controller method
def list
employees = Employee
.includes(:hourly_rates)
.where(hourly_rates: {active:true})
.order(Arel.sql("#{params[:column]}"))
render(partial: 'employees', locals: { employees: employees })
end
Thanks for any advice how I can implement this.
Dan