I am using the Geocoder gem in my RoR app to search a database of objects that may or may not have a address. When a user searches for these objects using a location, I want to show them both objects with an address near the location as well as objects that do not have an address. Currently I have something like this:
#objects = Object.near(location, 50)
This only returns objects within a 50 mile radius of location. Is there a way to include objects with address == nil in a single query?
Related
I have an Active Record Model called Owner. It is associated with both a House and a CoOwner. An Owner also has a column called debt_amount. In my case, every Owner will always be attached to both a House and a CoOwner
A house has a location field
I am looking to run a query to find all Owners attached to a specific co-owner, with nil for debt_amount. This is working fine.
Where I am running into trouble is when I attempt to sort this the associated Houses.location.
Below is what I am currently using.
Owner.includes(:house).where(co_owner_id: 10)
.where.not(debt_amount: nil)
.order([house.location])
Any ideas on how to make this work, or pointers to resources I can use to do some reading?
Owner.includes(:house)
.where(co_owner_id: 10)
.where.not(debt_amount: nil)
.order("houses.location ASC")
Here are more examples, https://apidock.com/rails/ActiveRecord/QueryMethods/order
With the last release of Quickblox SDK there is a new LOCATION field type, meaning we can save gps coordinates.
My question is: are or will be there specific functions/methods to query on this kind of field?
For instance, can I get all records in a CUSTOM OBJECT class that are located with 10 kms of my current location? - (based on a location field in the class)
Can I order the result by distance from my location?
These kind of queries are available in the LOCATION module.
Until now we were queering LOCATION to get something like "all users within 10 kms of my location" and then using those users ID's to query CUSTOM OBJECT and get specific information about those users.
With the new LOCATION field can we do this using only CUSTOM OBJECTS?
Try something like this:
NSMutableDictionary *params = [NSMutableDictionary new];
params[#"field_name[near]"] = #"34.0013123, 15.014915; 40000";
To avoid name conflicts, try to give a plural name to location field.
For my app:
user has_many images, image belongs_to user
image has_one location, location belongs_to image
Perhaps the location's fields should just be part of the image. But regardless, I'm trying to write this query in Rails:
SELECT image.caption, location.latitude, location.longitude
FROM image, location
WHERE location.image_id = image.id
AND image.user_id = 5
or alternatively, if it's easier:
SELECT image.*, location.*
FROM image, location
WHERE location.image_id = image.id
AND image.user_id = 5
How would I write this as an ActiveRecord query?
I think you want to read about Eager Loading Associations.
#images = Image.includes(:location).where("images.user_id = ?", 5)
This will find Image instances where user_id = 5. It then runs a 2nd query that will JOIN and build the associated Location instance (thats what the .includes(:location) will do for you).
This more closely matches your alternative query, as it does select all columns from images and location tables.
You can build an Array based on this containing a hash with only the keys you're interested in through something like this.
#hash_object = #images.collect { |i| { caption: i.caption, latitude: i.location.latitude, longitude: i.location.longitude } }
If you want to build this with only a single query, you can use .joins(:location) in combination with .includes(:location)
Image.joins(:location).includes(:location).where("images.user_id = ?", 5)
Important: This will omit Image instances who have no assoicated Location. You can modify the joins() a bit to help with this, but the above will have this omission.
If you really want only specific columns to be selected, read up on Selecting Specific Columns though there are warnings for the use of this
If the select method is used, all the returning objects will be read only.
and
Be careful because this also means you’re initializing a model object with only the fields that you’ve selected.
In Rails master (not out in 3.2.11) you can pass multiple columns to .pluck() but this appears to only be restricted to a single table (you wouldn't be able to get the locations table's :latitude and :longitude when plucking from Image). It's good to know about though.
I'm attempting to write a site in Rails where a user in a manufacturing plant can see what devices are failing. The program storing the alarm data stores one entry when a device faults, and then stores another entry when the device gets fixed. The entries are linked only by having the same value in the EventAssociationID column. How might I write a named scope in Rails to check which faults have been fixed and which ones haven't?
I wasn't able to do it in a named scope, however, I was able to define a method for the model that solved the problem:
def inAlarm
return ConditionEvent.count(:all, :conditions => ['EventAssociationID = ?', self.EventAssociationID]) == 1
end
I am very new to RoR so this may be very fundamental. My structure keeps getting a level deeper and I can't figure out how to find the id anymore.
First you have a Company which can have many Users. Users sign in and are authenticated and the current_user is saved in a cookie with the Session.
Since the User has one Company I can always find the Company.id through the current_user.
Next a Company has many Farms. In farms create I can get the company id from the user cookie and the farm id is new so that works, and in farm show Rails knows which farm it is supposed to show. So that level works.
Now I want to add that a Farm has many Blocks. I am adding Blocks through the associated Farm show page, but the Blocks_controller doesn't know what farm page it is on (as far as I can tell, if it can any info is appreciated).
Here is the FarmsController create that works:
def create
company_id = current_user.company_id
#company = Company.find(company_id)
#farm = #company.farms.build(params[:farm])
if #farm.save
flash[:success] = "farm created"
redirect_to root_path
else
render 'pages/home'
end
end
And this code just complains that it doesn't know what id I am talking about:
BlocksController
def create
#farm = Farm.find(params[:id])
#block = #farm.blocks.build(params[:block])
end
This is displaying on the associated Farm show page, so if there is a way to capture the id I would love to know what it is.
Thank you for your time.
The three easiest ways to get that id is to:
Pass in that farm_id using a hidden form field. When creating the link to your blocks/new form just pass in the farm_id ie use a path like new_blocks_path(:id => #farm.id) inside your blocks controller you will want to make sure that the farm_id is set on the Block model.
def new
#block = new Block
#block.farm_id = params[:farm_id]
end
Then if you are using form for the farm_id field (which should probably be of type hidden), it should contain the right id. Now change the first line in the "create" block method to
#farm = Farm.find(params[:block][:farm_id])
You can combine the process of adding the blocks and the farms using nested forms. Take a look at http://railscasts.com/episodes/196-nested-model-form-part-1 for how to do this.
You can use nested RESTful resources to make sure that within the blocks controller you always have access to the farm id. For more information about how to do this try take a look at http://railscasts.com/episodes/139-nested-resources