Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
RoR: how to deal with information in arrays
Ok, so I'm trying to fool around with the koala gem - to link facebook with my ruby on rails application. I'm starting to get the hang of it, but I'm running into some serious mental blocks!
def friends
facebook.fql_query("SELECT uid,username, is_app_user, name, pic_square FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user=1");
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil
end
So, firstly, I'm using the FQL to search through a users friends. It returns a hash of details of users who are friends of current_user (the one signed into the app) - and that have also connected with my application.
def friends_realname
names = friends.select {|f| f["is_app_user"] }.map {|f| f["name"]}
names.each { |name| puts name}
end
Here, I'm selecting the above "user's friends also using my app's" names - and mapping them into an array. This returns an array: ["friend1's name", "friend2's name"]
def friends_avatars
@avatars = friends.select {|f| f["is_app_user"] }.map {|f| f["pic_square"]}
end
Here, I'm trying to do the same - but returning the URLs of the profile pictures of those same users. It returns an array of ["http://www.example.com/example.jpg", "http://www.example2.com/example2.jpg"]
Here's where I'm stuck. I want to format the information from the returned arrays. So for the real names - instead of returning the actual array, I want to return a list of names. And obviously for the images, instead of returning an array of image urls - I actually want to 'puts' those images out in the view.
So, I'm thinking that I need to iterate through each of these arrays, and then use the .each method to do something with each string in the array - e.g puts img_tag? As I said, if anyone can help with some example code, I'd really appreciate it :)
Would something like this be possible?:
def friends_avatars
@avatars = friends.select {|f| f["is_app_user"] }.map {|f| f["pic_square"]}
@avatars.each { |avatar| puts image_tag "#{avatar}}"}
end
Cheers, Sam
EDIT: Just realised that image_tag is a helper method, so I can't use it there.
2 Answers
Jason Seifer
Treehouse Guest TeacherHey Sam,
This is a loaded question but I think you're on the right track. Personally, I'd take a slightly different approach to this and use the items as a hash structure instead.
First, I'd rename the friends method so it's a little more representative of what we're asking for:
def facebook_friends_query
facebook.fql_query("SELECT uid,username, is_app_user, name, pic_square FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user=1");
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil
end
Now we can create a friends method which uses that query method. Don't forget to write tests to make sure that the method we wrote above works:
def friends
@friends ||= facebook_friends_query.select{|f| f["is_app_user"] }
end
Now we have a friends method we can iterate through. The first thing we do is assign it to a variable with the conditional assignment operator. That will assign a value to a variable if it does not already have one. This way, the second time it runs it will return the friends. Also, we're reusing the f["is_app_user"] in the two examples above so we just shorten it to one method.
Now we're free to iterate over the friends hash and have access to the information it provides:
friends.each do |friend|
puts "Name: #{friend['name']}"
puts "Avatar: #{friend['pic_square']}"
puts "Avatar image tag: <img alt="#{friend['name']}" src='#{friend['pic_square]}' />"
end
Disclaimer: I've never used the Koala gem and this code may not work, but hopefully this makes sense :)
Hey Jason, really appreciate your help! It does indeed make sense :) The code you've suggested works perfectly :) Although, I do think I'm putting my code in the wrong places (model/view/controller)!
After I typed out this question, I spent all night fiddling around with things, and I ran into A LOT of errors. The main one was with regards to my variables (global or instance) being NilClass. Then when I tried to iterate through, what I thought was a hash or array (using .each for example) - I got back a NoMethod error.
I tried a few things - creating simple variables (e.g array = Array.new, or array = [ ]) - and use the class method on those variables - still NillClass. This has really confused me - as when I use the same .class method in IRB - I get back what I expect. I feel like this is a really important part of Rails variables that I'm just not clicking with - any ideas? When that variable is within a method - and I call class on that method - it is correct however, is this just the way that rails works?