I have imported a .csv file of 10,000 locations and I need to loop through the database and geocode_by a few fields rather than the usual "geocode_by :address"
I am using the geocoder gem.
My database scheme looks like this
create_table "locations", :force => true do |t|
t.string "Address"
t.string "City"
t.string "State"
t.string "Zip"
t.float "latitude"
t.float "longitude"
t.datetime "created_at"
t.datetime "updated_at"
end
Can I do this in a controller action rather than on validation?
Should I do something like this:
def index
#locations = Location.all
#locations.each do |l|
new_address = "#{l.Address} #{l.City} #{l.State}"
geocode_by = :new_address
end
end
But yea, any help is greatly appreciated, thank you!
I was able to get things working with this controller code:
def index
if params[:search].present?
#locations = Location.near(params[:search], 20, :order => :distance)
else
#locations = Location.all
#locations.each do |l|
if l.latitude.nil?
new_location = "#{l.HP_Address_1} #{l.HP_City} #{l.HP_State}"
s = Geocoder.search(new_location)
l.latitude = s[0].latitude
l.longitude = s[0].longitude
l.save
end
end
end
end
It loops through every entry in the database, and if the latitude and longitude values have not been encoded it calls the correct geocoder function, and stores the returned lat / long values in the database.
I have 9k entries in my database, so I can only encode 2k per day due to the limit on the google maps api. But for folks used to encoding entries during creating using geocoded_by in their models, this will work in controllers.
This is the kind of thing I'd do with workers (delayed job or resque), since it might take some time to complete. But I'm not sure I understood the question, so this might not be the kind of answer you're expecting.
There is a rake task for this if you want to do every record in a class.
rake geocode:all CLASS=YourModel sleep=0.25
Related
I have part of a rails application where a user will create a recipe that will be saved in their "cookbook". Other users will be able to take recipes from other users. So there will be an aspect in the application that shows who created the recipe.
Schema for a Recipe
create_table "recipes", force: :cascade do |t|
t.string "recipe_name"
t.string "description"
t.integer "calories"
t.integer "carbs"
t.integer "fats"
t.integer "protein"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Where I am having trouble is displaying the recipe's creator.
def show
#user = current_user
#recipe = Recipe.find_by(params[:id])
creator = User.find_by(params[#recipe.user_id])
#creator = creator.first_name
end
So for right now I have two user's John (Id: 1) and Alex (Id:2). When I have Alex make a recipe and I put a pry under #recipe I get a user_id of 2 when I call #recipe.user_id.
However, when I put the pry under creator and call creator I get the user_id of 1 and I get John. I believe something is wrong with how I am trying to find the user using the user_id in #recipe. I was wondering if anyone know what I am doing wrong or if I need to add more information. Thanks.
This:
User.find_by(params[#recipe.user_id])
Doesn't make sense for a couple of reasons:
find_by expects a hash-like structure. Something like: User.find_by(id: xxx)
params[#recipe.user_id] doesn't make sense because that's going to be something like: params[1] which is not what you want.
This:
#recipe = Recipe.find_by(params[:id])
Also suffers from the malformed find_by.
So, try something like:
def show
#user = current_user
#recipe = Recipe.find(params[:id])
creator = #recipe.user
#creator = creator.first_name
end
This, naturally, assumes you have your association between Receipt and User set up correctly (i.e., using belongs_to).
I'm trying to get a complicated piece of data from rails.
What I want are all users, the tasks that are associated with them and limited to specific project.
Here's the task schema for reference:
create_table "tasks", force: :cascade do |t|
t.date "start_date"
t.date "end_date"
t.integer "hours"
t.string "priority"
t.integer "project_id"
t.integer "user_id"
t.string "name"
t.string "description"
end
I accomplish parts of this with this call
users = User.includes(:tasks).where('tasks.end_date Between ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week).references(:tasks).where('tasks.start_date Between ? AND ?', Date.today.beginning_of_week, Date.today.end_of_week).references(:tasks).where('tasks.project_id = ?', #project.id).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }
My problem is that my query is not finding the tasks based on their dates correctly.
I am trying to find all tasks within a week range that either have a start_date or end_date within that week.
Is this possible within one query or do I require more advanced logic?
If you are using Rails 5 you can make user of or method
User.joins(:tasks).where(tasks: {end_date: Date.today.beginning_of_week..Date.today.end_of_week})
.or(
User.joins(:tasks).where(tasks: {start_date: Date.today.beginning_of_week..Date.today.end_of_week})
)
For sake of brevity, I haven't included project where clause.
I haven't tested this but I think what was happening was your where grabbing all of the users with tasks that end_date happens between given times and then querying those models with every user with tasks that start_date happens between given times. Giving you only users whose tasks start_date and end_date happen between the given times.
users = User.includes(:tasks).where('((tasks.end_date BETWEEN ? AND ?) OR (tasks.start_date BETWEEN ? AND ?)) AND tasks.project_id = ?', Date.today.beginning_of_week, Date.today.end_of_week, Date.today.beginning_of_week, Date.today.end_of_week, #project.id).references(:tasks).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }
Hope it helps. Cheers!
Here's what ended up working for me, thanks #vijay
User.includes(:tasks).where(tasks: {end_date:Date.today.beginning_of_week..Date.today.end_of_week}).or(User.includes(:tasks).where(tasks: {start_date: Date.today.beginning_of_week..Date.today.end_of_week})).where('tasks.project_id = ?', #project.id).map { |user| user.as_json.merge({tasks: user.tasks.as_json}) }
I would like to seed my Rails app database with the permutation of an existing array of objects, and am unsure about the best way to go about this.
I currently have a Country model, with the following attributes:
create_table :countries do |t|
t.string :name
t.float :latitude_dec
t.float :longitude_dec
t.timestamps null: false
end
I have seeded this model from a .yaml file (as these attributes are static), and now would like to use these records to seed a CountryPair model (where the attributes are also static). This model will have the following attributes:
create_table :country_pairs do |t|
t.string :country_a
t.string :country_b
t.string :pair_name
t.float :country_a_latitude_dec
t.float :country_b_latitude_dec
t.float :country_a_longitude_dec
t.float :country_b_longitude_dec
t.float :distance
t.timestamps null: false
end
The aim is to permutate the array of Country objects, and create a CountryPair object from each permutation (and seed the database with the output). I understand the Ruby array#permutation method, but am unsure about how to pull out the appropriate values into the new array of CountryPair objects. The order of countries in the pair is important here, so I'd like to use permutations rather than combinations.
Ultimately, I'd also like to calculate the distance between the country pairs, but I'm hoping to start figuring that out once I have the CountryPair model filled!!
This is my first foray back into Rails after a five year absence, so apologies if I've got some of the terminology/methodology wrong - please do ask for clarification if any further information is required! Thanks in advance!
You can add this snippet to your seeds.rb after the Countries are seeded.
Country.all.permutation(2) do |p|
CountryPair.create(
country_a: p[0].name,
country_b: p[1].name,
pair_name: p[0]name + p[1].name,
country_a_latitude_dec: p[0].latitude.dec,
country_b_latitude_dec: p[1].latitude.dec,
country_a_longitude_dec: p[0].longitude.dec,
country_b_longitude_dec: p[1].longitude.dec,
distance: # An algorithm to calculate distance
)
end
Then run it with: rake db:setup
I have the following code in my sessions controller which saves information such as Facebook friends, likes, and a user profile to my DB. The profile includes the user's location and gender but it gets saved into the DB as a string so I can't extract it.
#graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = #graph.get_object("me")
current_user.likes = #graph.get_connections("me", "likes")
current_user.friends = #graph.get_connections("me", "friends")
current_user.save
Going into the console, I can get the profile of the last user via:
u = User.last.profile
But this doesn't let me call for the location specifically, like:
User.last.profile.location
User table looks like
create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
t.string "likes"
t.string "profile"
t.string "location"
t.string "interests"
t.string "birthday"
t.string "activities"
t.string "friends"
end
like this?
graph = Koala::Facebook::API.new(#oauth_token)
#user = graph.get_object("me")
location = #user["location"]["name"]
I am using koala v2.2 and my application Facebook API Version is v2.4.
I did not get location information using just get_object("me") request. I had to pass location as fields parameter like
get_object("me?fields=first_name,last_name,location")
May be this information will be helpful for someone.
N.B: Of course you have to enable user_location scope.
The way I've used to work this out is using scopes and call these scopes in the view, which might not be the best way but is a quick fix.
The User model can be scoped by:
scope :CityName, where("profile like '%location: CityName%'")
to use Facebook Checkins since yesterday e.g.:
unless graph.get_connections('me', 'checkins', :since => 'yesterday').blank?
checkin = graph.get_connections('me', "checkins", :since => 'yesterday')
lat = checkin[0]['place']['location']['latitude']
long = checkin[0]['place']['location']['longitude']
This might seem like a duplicate question, but I can't find any information on this. I want to show the results from a remotely acquired json array excluding certain results by comparing them to a local table. I have a gallery model with:
t.integer :smugmug_id
t.string :smugmug_key
t.integer :category_id
t.string :category_name
t.string :description
t.integer :highlight_id
t.string :highlight_key
t.string :highlight_type
t.string :keywords
t.string :nicename
t.integer :subcategory_id
t.string :subcategory_name
t.string :title
t.string :url
The data for this model gets populated by a rake task that connects to the smugmug api (json) and stores the data locally. I'm trying to create a view that shows all the smugmug galleries that are not stored locally.
Here's what I've tried so far, but it's not excluding the locally stored galleries like I thought it would.
def self.not_stored
smugmug_list = Smug::Client.new.albums(heavy = true)
gallery_list = Gallery.select(:smugmug_id)
smugmug_list.each do |smugmug|
smugmug unless gallery_list.include? smugmug.id
end
end
Hopefully this makes sense. I'm getting a json array of galleries, and I want to display that array excluding results where the album id matches the smugmug_id of any of my locally stored records.
Quick edit: I'm using an adaptation of this gem to connect to the smugmug api.
Just use the difference operator.
General Example:
ruby-1.9.2-p136 :001 > [3,2,1] - [2,1]
=> [3]
So you would have:
smugmug_list.collect{|e| e.id} - gallery_list
Enumerable#collect will turn the smugmug_list into a list of id's. From there, you can do the difference operator, which will return all the id's of all the smugmug galleries that are not stored locally.
Another option to maintain the list of galleries:
smugmug_list.select{|e|!gallery_list.include?(e.id)}