Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to refactor complicated logic in create_unique method?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Darek Finster  
View profile  
 More options Oct 6 2012, 8:43 pm
From: Darek Finster <dariusz.fins...@gmail.com>
Date: Sat, 6 Oct 2012 17:43:09 -0700 (PDT)
Local: Sat, Oct 6 2012 8:43 pm
Subject: How to refactor complicated logic in create_unique method?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin Law  
View profile  
 More options Oct 8 2012, 3:29 pm
From: Colin Law <clan...@googlemail.com>
Date: Mon, 8 Oct 2012 20:28:18 +0100
Local: Mon, Oct 8 2012 3:28 pm
Subject: Re: [Rails] How to refactor complicated logic in create_unique method?
On 7 October 2012 01:43, Darek Finster <dariusz.fins...@gmail.com> wrote:

> 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

For a start I would do the x=... first since it does not depend on f.
Then you only need to do the find if x true.  Then in the find use
.count and test it for >0 rather then finding a record.  Also extract
the find out into one or more scopes.
Are there relationships between user and track?  If so then you may be
able to use current_user.tracks rather than testing user_id.

Colin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »