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

[WIP] Improve filtering by adding county/city models #102

Closed

Conversation

mmorava
Copy link
Contributor

@mmorava mmorava commented Jan 3, 2021

WIP for improving ad search by city/county

As a source of city/county data I used: https://uprava.gov.hr/o-ministarstvu/ustrojstvo/uprava-za-politicki-sustav-i-organizaciju-uprave/lokalna-i-podrucna-regionalna-samouprava/popis-zupanija-gradova-i-opcina/846

For the sake of simplicity i did not cater for "opcina/grad" difference so all locations are treated like Cities but if needed I can add support for that. I propose that we drop the "City" name and call it just 'Location' which belongs to a County

TODO: Proper PR description :)

Copy link
Collaborator

@vfonic vfonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if only covering the cities, and not also villages would be good enough. The hardest hit places are actually couple of villages around Petrinja. It would be misleading if the user would have to select "I'm requesting help in Petrinja", while that's actually not true. We could perhaps use your location suggestion to go around this... 🤔

Another issue is that the migration will raise error on live site because City.find_by_name won't find all the names that we currently have. I suggest we fix those manually if we decide to go this route:

        ad.update!(city: City.find_by_name(ad.city_zdel)) # might return nil, but won't fail

db/migrate/20210103181832_connect_ad_with_city.rb Outdated Show resolved Hide resolved
rename_column :ads, :city, :city_zdel
add_reference :ads, :city, foreign_key: true, index: true
Ad.all.each do |ad|
ad.update!(city_id: City.find_by_name(ad.city_zdel).id )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if the city is not found by the name and we definitely have cities in the database on live site that don't match any city name.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this would be ok

Suggested change
ad.update!(city_id: City.find_by_name(ad.city_zdel).id )
city = City.find_by(name: ad.city_zdel)
if city
ad.update!(city_id: city.id)
else
ad.update!(city_id: City.first.id)
puts "* No matching city for ad #{ad.id}."
end

After @vfonic normalisation I suppose we shouldn't have lot of this and we can correct them manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke about this with Vlado, we need to have normalized data first in the DB, any irregularities we can manually change. idea is for this to fail so it alert us we have mismatched data that we can than alter

Copy link
Owner

@vlado vlado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising, left few improvement suggestions. Looking forward for the UI :)

rename_column :ads, :city, :city_zdel
add_reference :ads, :city, foreign_key: true, index: true
Ad.all.each do |ad|
ad.update!(city_id: City.find_by_name(ad.city_zdel).id )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this would be ok

Suggested change
ad.update!(city_id: City.find_by_name(ad.city_zdel).id )
city = City.find_by(name: ad.city_zdel)
if city
ad.update!(city_id: city.id)
else
ad.update!(city_id: City.first.id)
puts "* No matching city for ad #{ad.id}."
end

After @vfonic normalisation I suppose we shouldn't have lot of this and we can correct them manually.

Comment on lines +1 to +2
class ConnectAdWithCity < ActiveRecord::Migration[6.1]
def change
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class ConnectAdWithCity < ActiveRecord::Migration[6.1]
def change
class ConnectAdWithCity < ActiveRecord::Migration[6.1]
Ad = Class.new(ActiveRecord::Base)
City = Class.new(ActiveRecord::Base)
def change

https://guides.rubyonrails.org/v3.2/migrations.html#using-models-in-your-migrations

ActiveRecord::Base.transaction do
Rake::Task['db:seed_locations'].invoke
rename_column :ads, :city, :city_zdel
add_reference :ads, :city, foreign_key: true, index: true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_reference :ads, :city, foreign_key: true, index: true
add_reference :ads, :city, foreign_key: true, index: true, null: false

Copy link
Collaborator

@vfonic vfonic Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work immediately, because we have ads in our database and, when we add city_id reference, none of the ads will have city_id that is not null.

We can do:

      add_reference :ads, :city, foreign_key: true, index: true
      # ... do the data migration
      change_column_null :ads, :city_id, false

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use this as opportunity to clean up data before we have too many ads

db/seeds.rb Outdated Show resolved Hide resolved
def change
create_table :cities do |t|
t.string :name
t.references :county, foreign_key: true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.references :county, foreign_key: true
t.references :county, foreign_key: true, null: false

@vlado
Copy link
Owner

vlado commented Jan 3, 2021

For the sake of simplicity i did not cater for "opcina/grad" difference so all locations are treated like Cities but if needed I can add support for that. I propose that we drop the "City" name and call it just 'Location' which belongs to a County

I think we can just keep calling them cities, even if they are not real cities.

I'm not sure if only covering the cities, and not also villages would be good enough. The hardest hit places are actually couple of villages around Petrinja. It would be misleading if the user would have to select "I'm requesting help in Petrinja", while that's actually not true. We could perhaps use your location suggestion to go around this... 🤔

From what I see the list @mmorava used has 500+ cities (cities and bigger villages). Full list with smaller villages from https://www.posta.hr/preuzimanje-podataka-o-postanskim-uredima-6543/6543 has 6000+ cities.

I think going with 500+ cities makes things simpler, you can enter village name in the address field Moja ulica 23, Majske Poljane (I suppose this should be enough to geolocate later).
6000+ could also work but not sure if it is worth the effort.

Looking forward for the opinions.

@mmorava
Copy link
Contributor Author

mmorava commented Jan 4, 2021

Added UI changes, resolved conflicts

Thanks for the comments, I will have a look at them in the morning and amend the code before I do a proper QA on this card

@vlado
Copy link
Owner

vlado commented Jan 5, 2021

@mmorava Please check #107

@mmorava
Copy link
Contributor Author

mmorava commented Jan 5, 2021

deprecated by #107

@mmorava mmorava closed this Jan 5, 2021
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

Successfully merging this pull request may close these issues.

4 participants