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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/0f9XTmZfNt0J.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Colin Law at Oct 8, 2012 at 7:28 pm

    On 7 October 2012 01:43, Darek Finster 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 received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    To view this discussion on the web visit
    https://groups.google.com/d/msg/rubyonrails-talk/-/0f9XTmZfNt0J.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 8, '12 at 6:39p
activeOct 8, '12 at 7:28p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Darek Finster: 1 post Colin Law: 1 post

People

Translate

site design / logo © 2021 Grokbase