I'm using this gem and I would like to get a user's vote count for posts
https://github.com/bouchard/thumbs_up/blob/master/lib/acts_as_voter.rb
However when I do
#user.vote_count
It returns the user's vote count for everything I have comments, photos, etc.
I want to do something like #user.vote_count.where('voteable_type = Micropost') but obviously that doesn't make any sense
Vote.find_by_sql("SELECT COUNT(*) FROM votes WHERE votes.voter_id = #{#user.id} AND votes.voter_type = 'User' AND votes.voteable_type = 'Micropost'").count
probably not the best to use SQL but it works
Related
I want to have some stats from my app, User can publish post that I call Idee, and I want to know how many user have publish at least one idee.
I'm assuming that should be something like:
#total_user_active = User.where(idee.size >= 1).count
But this doesn't work.
It's user who is in table idee so maybe I should count how much unique user_id are in this column but how?
You can do the following:
#total_user_active = Idee.distinct.count(:user)
My app allows users to see what others are selling (typically objects) around them.
In my first try, I get all objects using:
#objects = Object.all.order( created_at: :desc )
Now, I would like to restrict objects list to objects that are sold around the current user, using Geocoder gem. So I did:
#users = User.nearbys(10)
which gives me users around current user by 10kms. But I have to change the view because now I'm using a list of users instead of a list of objects.
How can I get a list of objects (like in try number 1) in a specific area (like in try number 2)?
Thanks
I finally got it:
#objects = Object.where( user_id: current_user.nearbys(10).map{ |u| u.id } )
Hope it will help.
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".
My app has Question models that has_many UserResponse models.
I'd like to get the last 5 questions that a particular User has answered with the associated UserResponse objects filtered on the user_id field.
Here's what I currently have:
Question.group("questions.id").
joins("inner join user_responses r on r.question_id = questions.id").
where("r.user_id = #{user_id}").
order("questions.id asc").limit(5)
This query gives me back what I want, but the problem is when I get a Question out of the array and do question.user_responses the result is all of the responses for that question, not just the ones that should be filtered by the join/where clause above.
I've tried to do this:
Question.includes(:user_responses).group("questions.id").
joins("inner join user_responses r on r.question_id = questions.id").
where("r.user_id = #{user_id}").
order("questions.id asc").limit(5)
thinking it would eager load each response...but it doesn't appear to function that way.
Where am I going wrong?
If it makes a difference, the reason I need everything eagerly loaded is because I want to take the array and call to_json on it and return the json from a web service for my mobile app, so I need the entire graph available.
I think you're trying to get too complicated here. How about the following?
Question
.includes(:user_responses)
.where("user_id = ?", user_id)
.order("questions.id asc")
.limit(5)
i'm creating a facebook-app for university project and i'm trying to store all my friends in the DB.
By using the API-syntax "me/friends" i get a facebook-respond looking like this:
{"data"=>[{"name"=>"Albert Einstein", "id"=>"11111111"}, {"name"=>"Max Mustermann", "id"=>"222222222"}, {"name"=>"Just Another Name", "id"=>"333333333"}]}
I believe its a json-object, but i'm not sure.
Question: How can i save the data, i need a DB with all the User-IDs of my friends.
Thx!
Edit:
Hey, this is what i have searched for. But i still get an error and don't know why.
My code:
def insert_1
fb_friends = rest_graph.get('me/friends')
fb_friends[:data].each do |f|
#friend = Model.new(:name => f["name"] )
#friend.save
end
end
I get an Heroku error (We're sorry, but something went wrong.)
You have two options -
Option 1-
You can create a friends table which will belong to users table. If a user has 200 friends, it will create 200 entries in friends table all belonging to the user via has_many-belongs_to relationship. For storing data, you just have to iterate over facebook friends hash and then save each of them separately
Pros : You can search for any friend separately.
Cons : There will be so many of friend entries. Saving them will take time, if somebody has many friends(say 500-700). Repeating entries will be created for mutual friends.
Options 2
You can add a friends column in your users table and declare this in your user.rb
serialize :friends
This way, you just have to pass a hash object to friends attribute of user table, and rails will save that in yaml format for you. When you will do #user.friends, rails will again convert that yaml formatted data to hash object and return it.
Pros : There will be only one query to save all users. You can iterate through this hash to show list of all friends.
Cons : You can't update them separately, you will update all together. Not good if you want to store some other information in relation to user's friends.
Update
as per your code example above
fb_friends = #your logic to get data as shown above.
fb_friends[:data].each do |f|
#friend = Friend.new(:name => f["name"],:fb_user_id => f["id"] )#creating Friend model obj.
#friend.save
end