I am having trouble finding a user's friends at a specific location using the current_location attribute. This is my query:
SELECT name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND
current_location = 103723936333579
This always returns empty even though I know that many of the user's friends are living at the location with id 103723936333579. I know that current_location is an array, but if I write:
SELECT name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND
current_location.id = 103723936333579
... to get the id of the array I get an error. The dot-notation doesn't seem to be working for me.
Thanks
Facebook FQL queries don't support querying by an array element like you are trying to do. The only way I can think of is to return all users and their current_location field and loop through every user and their current_location value.
You need to prompt the user for friends_checkins extended permission to make sure the current_location value is populated.
Related
(See example schema image below)
I am attempting to query a single user from the users table using the email field, along with the id & key fields from the applications table. The results should contain the user found (if any), along with the application (referenced using the key & id fields) and the applications_users associated data.
I can easily write SQL manually to perform this operation:
SELECT
"users".*,
"applications_users"."scopes",
"jwt_applications".*
FROM
"users"
INNER JOIN
"applications_users" ON "applications_users"."user_id" = "users"."id"
INNER JOIN
"jwt_applications" ON "jwt_applications"."id" = "applications_users"."application_id"
WHERE
"users"."email" = 'rainbows#unicorns.net'
AND "jwt_applications"."id" = '01daafc9-2169-4c78-83e9-37ac0a473e3d'
AND "jwt_applications"."key" = 'follow_the_rainbow'
LIMIT 1
However, I cannot for the life of me get the query correct when using ActiveRecord.
These are the unsuccessful attempts I have made thus far:
user = User.where(email: args[:username]).joins(:applications).merge(
JwtApplication.where(id: args[:application][:id], key: args[:application][:key])
).take!
This gets the user correctly, however Rails performs a second SQL query when I attempt to access user.applications (and it also returns all applications associated with the user; so it appears to disregard the id & key conditions)
user = User.where(email: args[:username]).joins(:applications).merge(
JwtApplication.where(id: args[:application][:id], key: args[:application][:key])
).references(:applications_users).take!
This gets the user correctly and also the correct application (yay!), however Rails performs a second SQL query if I attempt to call user.applications_users -- it also returns a collection for all data inside the applications_users table (again, disregarding the id & key conditions)
user = User.where(email: args[:username]).joins(:applications).where(
jwt_applications: {
id: args[:application][:id],
key: args[:application][:key]
}
).take!
This gets the correct user, however Rails performs another SQL query when I attempt to access user.applications -- also returning all applications.
Anyway, hopefully a Rails genius can shed some light on this question! I will be the first to admit that I am by no means a Rails expert; I have spent the last 10 years of my professional career coding in PHP & C++, so please bear with me if this comes off as a stupid question :)
Not sure if this is something you're looking for but...
You can write ActiveRecord query like (join model should be implicitly added to your query):
User.joins(:applications).where(email: email).where(applications: { key: key, id: id})
Where email, key and id as params to pass to the query.
On top of that query you can use select fields to get everything you need:
user = User.joins(:applications).where(email: email).where(applications: { key: key, id: id}).select('users.*, applications.id as appid applications.key as appkey').first
That will give you back the user model (if present) or empty relation if nothing matches your criteria.
You can then call the fields like
user.appid
user.appkey
You can always call select ('users.*, application_users.scopes, applications.*) which will return you all the fields in single instance (still under User model) BUT duplicate fields like id will only be shown once, that's why it's better to grab just the fields you want and give them unique identifiers like I've shown with appid and appkey.
Again, might not be exactly what you're after, but hopefully it points you in the right direction!
I am implementing search functionality in rails. When i search for users, the logged in user who is searching also comes in search. I want to avoid it.
This is in my search_method in controller
#matchedUsers = InUser.where("first_name like ?", "%#{params[:searchfnameInput]}%")
And i have user id in session[:user_id]. I want to exclude the record having id==session[:user_id] from #matchedUsers?
Thanks and Regards
Add another where clause to your query:
#matchedUsers = InUser.where("id<>?", current_user.id).where("first_name like ?", "%#{params[:searchfnameInput]}%")
The "id<>?" says "exclude the user with this ID".
I'm fetching my friends using the Koala gem for Rails framework like that:
#friends = current_user.facebook.get_connections("me", "friends?fields=id,name,picture.type(small)")
The question is: how do I fetch my friends ordered by name, for example ?
Use FQL or post process it in Ruby.
So for FQL in Koala, something like
current_user.facebook.fql_query(my_fql_query)
Where my_fql_query is
SELECT uid, name, pic_small FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY name
I want to get all the checkins a user is tagged in (note: I am not interested in his own checkins). I tried the following, which is a little illogical and of course does not work, but you'll get what I am trying to do:
SELECT message FROM checkin WHERE tagged_uids IN
(SELECT uid FROM user WHERE uid = me())
Any ideas?
You're thinking of IN backwards. The query you're looking for is:
SELECT message FROM checkin WHERE me() IN tagged_uids
However, tagged_uids is not indexable, so you'll need to know more information before you can run this query (like who is actually recording the checkin). One thing you could try is:
SELECT message FROM checkin
WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
AND me() IN tagged_uids
This will find all checkins in which your user is tagged that are by friends of the user (probably the only people who can check in that user anyway).
How to programmatically find which of my friends are using my application using Mogli or Facebooker2.
If the only way is to use FQL can someone provide an example?
I think the code bellow is able to find the
select uid, name, is_app_user from user where uid in (select uid2 from friend where uid1=me()) and is_app_user=1
Just tested:
https://api.facebook.com/method/fql.query?query=select+uid%2C+name%2C+is_app_user+from+user+where+uid+in+%28select+uid2+from+friend+where+uid1%3Dme%28%29%29+and+is_app_user%3D1%0A
&access_token=YOUR_ACCESS_TOKEN
&format=json
And is returning the correct results so FQL is actually working!