I would like to simplify this complicated logic for creating unique Track object.
def self.create_unique(p)
f = Track.find :first, :conditions => ['user_id = ? AND target_id = ? AND target_type = ?', p[:user_id], p[:target_id], p[:target_type]]
x = ((p[:target_type] == 'User') and (p[:user_id] == p[:target_id]))
Track.create(p) if (!f and !x)
end
Here’s a rewrite of with a few simple extract methods:
This rewrite shows my preference for small, descriptive methods to help explain intent. I also like using guard clauses in cases like
create_unique; the happy path is revealed in the last line (create(attributes)), but the guards clearly describe exceptional cases. I believe my use ofexists?inexists_for_user_and_target?could be a good replacement forfind :first, though it assumes Rails 3.You could also consider using uniqueness active model validation instead.