3
\$\begingroup\$

I have this method in my rails app:

def find_or_create_session(fbid)
 @sessions = Session.all
 if @sessions.find_by facebook_id: fbid
 @session = @sessions.find_by facebook_id: fbid
 if ((Time.now - @session.last_exchange).fdiv(60)).to_i > 5
 @session = Session.create(facebook_id: fbid, context: {})
 end
 else
 @session = Session.create(facebook_id: fbid, context: {})
 end
 @session
end

It retrieves all the Sessions, and tries to find one with a given facebook_id. If there is one, it checks if it's a recent one (last_exchange was less than 5 minutes ago). If it's not, it creates a new one.

If it can't find a session for this facebook_id, it will create one.

This is pretty easy, but it seems to be a lot of if/else for such a straightforward thing.

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Apr 26, 2016 at 13:40
\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

Some notes:

  • @session: A method called find_or_create_session should not change the state of instance variables.
  • @sessions.find_by facebook_id: fbid is written twice. Use variables to avoid repetition. Also, the consensus in the Ruby community is to always write parens (except for DSL-like code).
  • Time.now - @session.last_exchange: You can perform that check in the SQL query.
  • Your method is basically doing find_or_create_session = existing_session || new_session, let the code mimic it.

I'd write:

def find_or_create_session(fbid, max_age: 5.minutes)
 Session.find_by(["facebook_id = ? AND last_exchange >= ?", fbid, max_age.ago]) ||
 Session.create(facebook_id: fbid, context: {})
end
answered Apr 26, 2016 at 22:18
\$\endgroup\$
2
\$\begingroup\$

I believe this is equivalent:

def find_or_create_session(fbid)
 if (@session = Session.find_by facebook_id: fbid) &&
 (Time.zone.now - @session.last_exchange).fdiv(60).to_i <= 5
 @session
 else
 @session = Session.create(facebook_id: fbid, context: {})
 end
end

Some notes:

  • No need for an @sessions variable unless you'll be using it in the views
  • Always use Time.zone.now instead of Time.zone
answered Apr 26, 2016 at 14:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.