New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scope with multiple arguments #39
Comments
@spawnge it looks like the # select options, set in controller:
[['2 km', 2000], ['5 km', 5000], ...] # nested form field in view:
<%= f.fields_for :with_distance do |with_distance_fields| %>
<%= with_distance_fields.hidden_field :lat, value: current_user.lat %>
<%= with_distance_fields.hidden_field :lng, value: current_user.lng %>
<%= with_distance_fields.select :distance_in_meters, @filterrific.select_options[:with_distance] %>
<% end %> Instead of overriding the value in the form, you could also initialize the @filterrific object in the controller with the user's position params. See issue #27 for more info. |
Hi @jhund thanks a lot for your answer. I tried to implement your solution but it doesn't seem to work with the scope I have (that I copied from this tutorial) Can you take a quick look at it ?
Or how can I initialize the @filterrific object with the user's position params? Sorry for all these questions I'm still a beginner :) |
@spawnge here is what I would do: In the model the with_distance_attrs are passed as a single hash, so you need to change the parameters for the scope: scope :with_distance, -> (with_distance_attrs) {
['lng' => '-123', 'lat' => '49', 'distance_in_meters' => '2000']
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || courses.lng || ' ' || courses.lat || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [with_distance_attrs['lng'], with_distance_attrs['lat'], with_distance_attrs['distance_in_meters']])
} Then the form looks like this. I assign values for lat and lng directly from current_user, rather than initializing the @filterrific object with it: <%= f.fields_for :with_distance do |with_distance_fields| %>
<%= with_distance_fields.hidden_field :lat, value: current_user.lat %>
<%= with_distance_fields.hidden_field :lng, value: current_user.lng %>
<%= with_distance_fields.select :distance_in_meters, @filterrific.select_options[:with_distance] %>
<% end %> So the only difference to what you have posted is that the |
My issue is fairly simple, in my app I have this scope that retrieves all the courses within a certain distance with the help of a postgis database:
scope :with_distance, -> (lat, lng, distance_in_meters) {...}
The scope is working but I don't know how to write the select options for a scope that has more than one argument. I wrote this huge mess that's not even working (wrong number of arguments) but you get the idea:
[ [['2 km'], [current_user.lat, current_user.lng, 2000]], [['5 km'], [current_user.lat, current_user.lng, 5000]] ]
I've been trying to solve this for a long time, any suggestion would be greatly appreciated
The text was updated successfully, but these errors were encountered: