Skip to content
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

Closed
btazi opened this issue Mar 17, 2015 · 3 comments
Closed

scope with multiple arguments #39

btazi opened this issue Mar 17, 2015 · 3 comments

Comments

@btazi
Copy link

btazi commented Mar 17, 2015

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

@jhund
Copy link
Owner

jhund commented Mar 18, 2015

@spawnge it looks like the lat and lng params are attributes of the current_user. So you don't need to include them in the select options. Why don't you just store them in two hidden form fields? Then your select options contain just the various distances:

# 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.

@btazi
Copy link
Author

btazi commented Mar 18, 2015

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 ?

  scope :with_distance,  -> (lat, lng, distance_in_meters) {
  where(%{
    ST_DWithin(
      ST_GeographyFromText(
        'SRID=4326;POINT(' || courses.lng || ' ' || courses.lat || ')'
      ),
      ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
      %d
    )
  } % [lng, lat, distance_in_meters])
}

Or how can I initialize the @filterrific object with the user's position params? Sorry for all these questions I'm still a beginner :)

@jhund
Copy link
Owner

jhund commented Mar 21, 2015

@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 with_distance scope receives a single argument, rather than three.

@jhund jhund closed this as completed Apr 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants