'Receiving an ActiveRecord::Relation object
I have a method that is used to sort columns of a table. Jenkin fail on a Rspec which its method implemented this way:
def exec
my_requests = MyRequest.joins(user: :roles)
return my_requests.group(:id,'users.first_name','users.last_name','roles.title').order(order_binds) unless search_query.any?
my_requests.where(sanitized_sql).group(:id,'users.first_name','users.last_name','roles.title').order(order_binds)
end
Before I add 'roles.title' I was getting a passing build and tested sorting functionality successfully, but not sure what is the issue when I add roles.title. It output the below error. Looks like it returns an #<ActiveRecord::Relation [actual result] > instead of the [actual result]:
expected: [#<MyRequest id: 27, requestor_id: 93, sample_name: nil, item_numbers: ["SID123123XYZ"], written_at_begin...at: "2022-02-25 04:41:12", updated_at: "2022-02-25 04:41:12", PMI_numbers: [], request_status: nil>]
20:43:48
got: #<ActiveRecord::Relation [#<MyRequest id: 27, requestor_id: 93, sample_name: nil, item_numbers: ["SID123123XYZ"], written_at_begin...at: "2022-02-25 04:41:12", updated_at: "2022-02-25 04:41:12", PMI_numbers: [], request_status: nil>]>
Solution 1:[1]
You can try to use to_ary (alias to_a) method on the relation to convert it to an array. https://apidock.com/rails/ActiveRecord/Relation/to_ary
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stolarz |
