Skip to content

Instantly share code, notes, and snippets.

@oliyoung
Created July 14, 2011 00:58
Show Gist options
  • Save oliyoung/1081662 to your computer and use it in GitHub Desktop.
Save oliyoung/1081662 to your computer and use it in GitHub Desktop.
if at_place
@reward = place.rewards.cellar_door.active.first
else
@reward = place.rewards.regular.active.first
end
@oliyoung
Copy link
Author

'rewards' is a relationship, 'cellar_door', 'regular' and 'active' are scopes on reward

@ELLIOTTCABLE
Copy link

@reward = (at_place ? 
             place.rewards.cellar_door :
             place.rewards.regular)        .active.first

My preferred stylization is a bit … unusual. You get the basic idea, though.

If you really want to Take It Too Far™, then:

@reward = place.rewards.send(at_place ? :cellar_door : :regular).active.first

@seangeo
Copy link

seangeo commented Jul 14, 2011

I would suggest that either Place or Reward needs a method to pick the right reward based on whatever conditions it likes.

Assuming there is in some sort of user class that defines in_place

def reward_for(user)
  if user.at_place
    self.rewards.cellar_door.active.first
  else
    self.rewards.regular.active.first
  end
end

This wraps up the ugly and you can call it like:

@reward = place.reward_for(user)

All depends on your context though. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment