I have rails application, which deals with some google map related stuff.the problem is , i have a table which contains latitude & longitude columns. the column types are "float". for some occasions i need to generate the query by the following:
clients.find_all_by_lat_and_lng(latvalue,lngvalue).
I gave the correct & existing lat & lng values to fetch the database value. but i can get only Empty.
I tried with difference dynamic finders like find_by_lat,find_by_lng i get only empty values from the query ...but data are exits in the table.
I guess the Float column type is the culprit here, i don't how to overcome this and get the values. Can one suggest me on this.
NOTE : if i work with same query in windows i get the values. my box is ubuntu here i can't get the values
clients.find(:all, :conditions => ["lat = ? and lng = ?",latvalue, lngvalue])
try this your model name is "clients" ???
or Client
Related
I'm using Rails to search through a SQLite table (for other reasons I can't use the standard database-model system) using a SELECT query like so:
info = ActiveRecord::Base.connection.execute("SELECT * FROM #{form_name} WHERE EmailAddress = \"#{user_em}\";")
This returns the correct values, but for some reason the output is in duplicate, the difference being the 2nd set doesn't have column titles in the hash, instead going from 0-[num columns]. For example:
{"id"=>1, "Timestamp"=>"2/27/2017 14:26:03", "EmailAddress"=>"-snip-", 0=>1, 1=>"2/27/2017 14:26:03", 2=>"-snip-"}
(I'll note the obvious- there's only one row in the table with that information in it)
While it's not exactly a fatal problem, I'm interested as to why it's doing so and if it's possible to prevent it. Thanks!
This allows you to read the values both by column index or column name:
id = row[0]
timestamp = row["Timestamp"]
I am trying to group records based on a value from a column, so I can use it to display the information elsewhere. At the moment I have this working if I specify the values in the column -
#city_count = People.select('city,count(*)').where("city in ('london', 'paris')").group(:city).count
This works fine if I want a list of people in London and Paris but if the city list also has Sydney, New York, Rio etc I don't want to keep adding the extra cities to the 'city in', I would like this to just find the people selected by each city.
Does anyone know the best way of doing this? Also if it can include NULL values as well.
Just use:
#city_count = People.group(:city).count
to get counts for all cities. This will include an entry for nil.
A more efficient way would be to use the distinct and count methods together.
#city_counts = Person.distinct.count(:city)
That way the work is done in the db instead of in Ruby.
I'm having an issue on this table: 1g_ydg74ooUSBzNfQBHOIdgrOKhxZD_92In8xTDg
I'm trying to fetch some results by CODE_DEPT. I used the filter
CODE_DEPT IN ('001', '002', '003', '02A', '02B')
and only the 3 first are fetched.
Any idea what's going on ?
Cheers
Looks like CODE_DEPT is identified as a numeric column. The last two codes are not numeric and so would not match anything. If you change the column to type Text and you should be OK.
So I am pulling my hair over this issue / gotcha. Basically I used find_by_sql to fetch data from my database. I did this because the query has lots of columns and table joins and I think using ActiveRecord and associations will slow it down.
I managed to pull the data and now I wanted to modify returned values. I did this by looping through the result ,for example.
a = Project.find_by_sql("SELECT mycolumn, mycolumn2 FROM my_table").each do |project|
project['mycolumn'] = project['mycolumn'].split('_').first
end
What I found out is that project['mycolumn'] was not changed at all.
So my question:
Does find_by_sql return an array Hashes?
Is it possible to modify the value of one of the attributes of hash as stated above?
Here is the code : http://pastie.org/4213454 . If you can have a look at summarize_roles2() that's where the action is taking place.
Thank you. Im using Rails 2.1.1 and Ruby 1.8. I can't really upgrade because of legacy codes.
Just change the method above to access the values, print value of project and you can clearly check the object property.
The results will be returned as an array with columns requested encapsulated as attributes of the model you call this method from.If you call Product.find_by_sql then the results will be returned in a Product object with the attributes you specified in the SQL query.
If you call a complicated SQL query which spans multiple tables the columns specified by the SELECT will be attributes of the model, whether or not they are columns of the corresponding table.
Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
> [#<Post:0x36bff9c #attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
Source: http://api.rubyonrails.org/v2.3.8/
Have you tried
a = Project.find_by_sql("SELECT mycolumn, mycolumn2 FROM my_table").each do |project|
project['mycolumn'] = project['mycolumn'].split('_').first
project.save
end
When I add:
fields: [ "doc['Location'].lon" ]
to my query, the longitude value is different that what is shown in the document source.
Here you can see the results of a query where I fetched the doc['Latitude'].lon and .lat, and the _source.Latitude to compare:
https://gist.github.com/d9533170f1f50fd27e87 (note - these have been passed through json_decode in PHP, but the data is the same before using json_decode.)
I first noticed this when I was using "doc['field_name'].distance(lat, lon)" to try and add the distance as a field to my query. I tried both the "script_fields" and "fields" keys and each had the same result.
UPDATE: I noticed that the "doc['Location'].lon" is returning what I thought should be the doc['Location'].lat (the lat and lon are switched.)
The problem was that when using GeoJSON format (or using lat/long as an array) you have to switch the order of lat/lng to lng/lat.
I am rebuilding my index, but, in order to work around this I have used this query for now:
doc['Location'].distance( lon + 180, lat ) // Temporary bandaid
Once I've rebuilt the index with the correct values I'll switch back to:
doc['field_name'].distance(lat, lon) // The correct method