SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with conditional 'unless' statement

    I need help getting my 'unless' statement to work. It's checking to see whether or not a db entry with the same date and user info already exists before it creates a new one.

    Unfortunatley, it doesn't seem to work. It allows duplicate entries to be made.

    Code Ruby:
    class Tweet < ActiveRecord::Base
      def self.get_latest
        Twitter::Search.new.hashtag("somesearch").fetch.each do |tweet_results| #hit the API.
          twitter_created_at = DateTime.parse(tweet_results.created_at)
          unless Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', DateTime.parse(tweet_results.created_at), tweet_results.from_user_id_str])
            Tweet.create!({
              :from_user => tweet_results.from_user,
              :from_user_id_str => tweet_results.from_user_id_str,
              :profile_image_url => tweet_results.profile_image_url,
              :text => tweet_results.text, 
              :twitter_created_at => twitter_created_at 
              })
          end
        end
      end
    end

    Ideas?

  2. #2
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to provide an exit action; you haven't created parameters for what occurs if a duplicate is found in your DB.

    Perhaps if you use an if/else statement:

    Code Ruby:
    if Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', DateTime.parse(tweet_results.created_at), tweet_results.from_user_id_str])
          prints "Generic Error Message" #This would be your response to a duplicate
    else
          Tweet.create!({
              :from_user => tweet_results.from_user,
              :from_user_id_str => tweet_results.from_user_id_str,
              :profile_image_url => tweet_results.profile_image_url,
              :text => tweet_results.text, 
              :twitter_created_at => twitter_created_at 
              })
          end
        end
      end
    end

    Also, you seem to have 2 variables buried in a string with a #{} to separate them from the string. Is that intentional?
    Last edited by Mittineague; Feb 4, 2011 at 00:27. Reason: reformatting bbcode tags

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correction:

    Also, you seem to have 2 variables buried in a string WITHOUT a #{} to separate them from the string. Is that intentional?

  4. #4
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @wadjorlolo: the #{} syntax shouldn't be used for passing variables into database queries, as Jon is doing here. The reason is that SQL code could be injected into the string, and your app will pass it straight on to the database, which could open you up to attacks.

    The syntax Jon is using is more secure, and is described in detail here: http://guides.rubyonrails.org/active...ray-conditions

  5. #5
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Louis Simoneau View Post
    @wadjorlolo: the #{} syntax shouldn't be used for passing variables into database queries, as Jon is doing here. The reason is that SQL code could be injected into the string, and your app will pass it straight on to the database, which could open you up to attacks.

    The syntax Jon is using is more secure, and is described in detail here: http://guides.rubyonrails.org/active...ray-conditions
    Thanks for the correction, I am currently parsing my way through The Rails 3 Way. Very informative.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •