Thanks a lot for your help.
I have a locations
and ads
table. Location has_many :ads
I perform the following query on Location Model.
@locations = Location.joins(:ads).where(@location_params.require(:location).permit(:id)).includes(:ads)
Then I want to perform an additional query on @locations
and filter it based on :ads
@locations = @locations.joins(:ads).where(ads: @ads_params.require(:ads).permit(:id)).includes(:ads)
This query does not work. It returns an empty object.
=> #<Location::ActiveRecord_Relation:0x1e29270>
This are the parameters:
location_params
@location_params.require(:location).permit(:id) <ActionController::Parameters {"id"=>1} permitted: true>
ads_params
@ads_params.require(:ads).permit(:id) <ActionController::Parameters {"id"=>1} permitted: true>
It is not permitted to give a specific column input like this:
@locations = Location.joins(:ads).where(locations: {id: 1}, ads: {id: 1})
I have postgresql, I did not think about using SQL, but I am not sure.
Query Parameters
I am using rails console to do this query, for testing.
@location_params
=> <ActionController::Parameters {"location"=><ActionController::Parameters {"id"=>1} permitted: false>} permitted: false>
@location_params.require(:location).permit(:id)
=> <ActionController::Parameters {"id"=>1} permitted: true>
@ads_params
=> <ActionController::Parameters {"ads"=><ActionController::Parameters {"id"=>1} permitted: false>} permitted: false>
@ads_params.require(:ads).permit(:id)
=> <ActionController::Parameters {"id"=>1} permitted: true>
binding.pry
, then in the console run@locations.joins(:ads).where(ads: ads_params).first
and see the invalid PG statement error, which you have to fix. – Libertine.first
and I receive theTypeError: cant't quote ActionController::Parameters
– BenzineLocation.joins(:ads).where(ads: ads_params.permit!.to_h
– Libertineto_h
is needed as you said, but I think.permit!
is not needed again. Thanks for your suggestion, you can add your answer. – Horacehoraciopermitted: true
, thuspermit!
is redundant. There is no sense in adding an answer which will repeat the one already given :) – Libertine.to_h
method (also with .permit!). I tested that it worked then he deleted the comment, maybe you 2 found the solution almost at the same time. – Benzineto_h
after your comment, I was a bit late to add saw your comment later on binding.pry result. This is a Rails 5 specific problem though #34950005 – Horacehoracio