Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:12:15+00:00 2026-06-12T10:12:15+00:00

I would like to simplify this complicated logic for creating unique Track object. def

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:12:17+00:00Added an answer on June 12, 2026 at 10:12 am

    Here’s a rewrite of with a few simple extract methods:

    def self.create_unique(attributes)
      return if exists_for_user_and_target?(attributes)
      return if user_is_target?(attributes)
    
      create(attributes)
    end
    
    def self.exists_for_user_and_target?(attributes)
      exists?(attributes.slice(:user_id, :target_id, :target_type))
    end
    
    def self.user_is_target?(attributes)
      attributes[:target_type] == 'User' && attributes[:user_id] == attributes[:target_id]
    end
    

    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 of exists? in exists_for_user_and_target? could be a good replacement for find :first, though it assumes Rails 3.

    You could also consider using uniqueness active model validation instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Obviously, I'm not an expert in C#. I would like to simplify this code
I want to create two sites on RubyOnRails and would like to simplify this
I would like to simplify this code and would love any suggestions. Structure of
I would like to simplify my main build scripts, and I'd like to have
I would like to simplify my code and add the possible to the css
I would like to use some iteration control flow to simplify the following LaTeX
I would like to simplify my JSP's even further by transparently including them. For
I have a rather stupid PHP question :D! I would like to simplify the
I would like to simplify my jQuery code for a footer popup/tooltip animation because
In a nutshell, I would like to simplify some of my MVC pages by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.