9 Replies - 2919 Views - Last Post: 07 August 2012 - 09:49 PM Rate Topic: -----

#1 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 04:51 PM

I have a class already mapped out and in a database through DataMapper and now I'm trying to make my first resource into the database.

I have a class that handles the form data and file stuff. In that class, I'm creating the first resource with @variables passed in from the params. All other args passed into this resource come from @variables that have values from the form. In this case, @url, the variable in question, is set to a value only a few lines before. Now when I put in the URL:

    rec = Post.new(
          # more args
          :filename_ogg => @url
    )
    rec.save


This is the killer: Every other line of code in this file is able to access @url, through a global variable ($upload = Upload.new(file)), except for this resource creator. When it comes to saving the resource, it doesn't go through. BUT, when I replace @url with a static string like "RANDOM URL.", it works perfectly. Why?

This had been tested under both MRI 1.9.3 and JRuby 1.6.7.2 (1.9 mode) under Ubuntu 12.04:
    
    # #{user} edited out, but the actual bucket name is in the actual file
    class Upload
    	attr_accessor :file, :filename, :filename_ogg, :status, :title, :desc, :url
    	
    	def initialize(file)
    		@file = file
    		@filename = @file[:filename].gsub(" ", "")
    		@filename_ogg = "#{@filename}.ogg"
    		#@url = "http://s3.amazonaws.com/#{user}/#{@filename_ogg}"
    
    	end
    	
    	def downandup
    		# code
    	end
    	
    	def convert(file, file_ogg)
    		# code
    	end
    	
    	def upload(file_ogg)
    		# code
    		@url = "http://s3.amazonaws.com/#{user}/#{file_ogg}"
    		
            # title and desc are accessed through $upload.title/$upload.desc
    		rec = Post.new(
    			:title => @title,
    			:description => @desc,
    			:author_id => Random.rand(5),
    			:time_uploaded => Time.now,
    			:filename_ogg => @url,
    			:comments_table => Random.rand(10),
    		)
    		rec.save
    	end
    end


The file runs through fine, but when it comes for DataMapper to put it in the database, it won't go in, but when replaced with the static string, the data gets stored.

Is This A Good Question/Topic? 0
  • +

Replies To: Ruby not allowing dynamic strings as an argument

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 877
  • View blog
  • Posts: 3,122
  • Joined: 12-May 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 04:56 PM

View Postheyoman1, on 07 August 2012 - 07:51 PM, said:

When it comes to saving the resource, it doesn't go through.

To answer your question, we need to know what "It doesn't go through" means. The case is never that "nothing happens." Something returned an unexpected value, you got an exception, what happened?
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 05:04 PM

What exactly does @url contain? Have you tried logging @url.inspect somewhere? Any special characters?

I assume replacing :filename => @url with :filename => "http://s3.amazonaws.com/#{user}/#{file_ogg}" doesn't change anything?

When you say "it won't go in", I assume save returns false? Are there any validation errors?

View Postxclite, on 08 August 2012 - 01:56 AM, said:

To answer your question, we need to know what "It doesn't go through" means. The case is never that "nothing happens." Something returned an unexpected value, you got an exception, what happened?


I assume in this case "It doesn't go through" means that no new entry was added to the database.
Was This Post Helpful? 0
  • +
  • -

#4 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 05:22 PM

View Postsepp2k, on 07 August 2012 - 07:04 PM, said:

I assume in this case "It doesn't go through" means that no new entry was added to the database.

Yup, it doesn't show up in the database, as compared to the static string.

Oh, and Upload isn't a DataMapper resource, Post is and it's already defined and part of a database.

This post has been edited by heyoman1: 07 August 2012 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon



Reputation: 1335
  • View blog
  • Posts: 3,398
  • Joined: 28-November 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 06:57 PM

http://rors.org/2008...cape-in-strings

Try alternate methods of quoting, especially when you're not for sure what it'll return.
Was This Post Helpful? 0
  • +
  • -

#6 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 08:42 PM

Nope, string quotes don't work either. I have a screeny here that shows that the url does come out to a real string, both before and after creating the resource (output is in the terminal at the bottom). Note: I have Recording.create here, but that does the same thing as Recording.new ; Recording.save

Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 877
  • View blog
  • Posts: 3,122
  • Joined: 12-May 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 08:55 PM

One thing with datamapper is to check the max length of that column in your schema. I'll get denied entries for filepaths often that are longer than a max length, for example.
Was This Post Helpful? 2
  • +
  • -

#8 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 09:21 PM

View Postxclite, on 07 August 2012 - 10:55 PM, said:

One thing with datamapper is to check the max length of that column in your schema. I'll get denied entries for filepaths often that are longer than a max length, for example.

THANK YOU! THANK YOU! THANK YOU! I can't believe something so small made my life chaotic for a day. I just couldn't figure out why, but THIS! I would have never thought about this! Thank you! :D
Was This Post Helpful? 0
  • +
  • -

#9 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 877
  • View blog
  • Posts: 3,122
  • Joined: 12-May 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 09:25 PM

You really don't want to know how many hours I've wasted figuring that one out.
Was This Post Helpful? 2
  • +
  • -

#10 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon



Reputation: 1335
  • View blog
  • Posts: 3,398
  • Joined: 28-November 09

Re: Ruby not allowing dynamic strings as an argument

Posted 07 August 2012 - 09:49 PM

View Postxclite, on 07 August 2012 - 11:25 PM, said:

You really don't want to know how many hours I've wasted figuring that one out.


We've all been there. It's a dark and cold place...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1