mongobongo
mongobongo

Reputation: 171

Writing conditionals in rails

I need to write a conditional in order to find users without facebook id's and then have a image path set for those users. Here is the code so far.

# load rails
    require '../../config/boot.rb' 
    require '../../config/application.rb'
    Rails.application.require_environment!

    users = User.order('id ASC').limit(500)

    users.each do |user|
        facebook_id = user.facebook_id

        unless facebook_id.blank?
            puts facebook_id.size
        end
    end

I was thinking maybe an if/then conditional so if the number is 0 (meaning blank) then input https://graph.facebook.com/ + facebook_id + '/picture?width=120&height=120 for that user.

Maybe something like:

   if facebook_id.blank
        then input.('https://graph.facebook.com/' + facebook_id + '/picture?width=120&height=120')

Upvotes: 0

Views: 80

Answers (1)

ichigolas
ichigolas

Reputation: 7725

For the query part this is the way to go:

User.where(facebook_id: nil) #=> returns all users without facebook_id

About the path, you were close:

class User < ActiveRecord::Base # reopen User class and define a new instance method
  def poster_path
    if self.facebook_id
      "https://graph.facebook.com/" + self.facebook_id + "/picture?width=120&height=120"
    end
  end
end

Upvotes: 1

Related Questions