nil in fixtures

a note:
in test/fixtures, when you write

name:
id: 34
name: "Roger"
number: nil

this actually assigns number as 0! This means that if you test in your model code for “number.blank?” this will now return true, since it is actually zero. The use of nil is truly misleading here, and should be done away with, in my opinion. So you can instead just call
...
if !user and !filename
...
do whatever in your model “verify” methods, and nil will work. Don’t use blank, basically.

end
instead of
if user.blank? and filename.blank?
do whatever
end

How to serve a file in RoR

# this is how to send it out–through Ruby. Unfortunately it appears that webrick buffers all output, so…with a 1 gig file…that is not good–it runs out of memory.
# send_file(realFileCompleteName,
# :filename => @user_blended_cast.name,
# :type => ‘image/gif’,
# :disposition => ‘attachment’,
# :streaming => ‘false’ # If false, the entire file is read into server memory and sent to the client. Otherwise, the file is read and written to the client in :buffer_size chunks. [from Agile Web Development p. 431]

# :buffer_size => ‘4096’
# }

Ruby starting a class as a thread

Here’s a function to start a class as if it were a thread (a la python)

def startGoFunctionAsThread()
return Thread.new() { ||
go() # this is defined somewhere else in the class
}

end
Remember if you run this as

a = objectName. startGoFunctionAsThread

to run a.join to wait for it to complete.

Ruby exception handling

how to do ruby try/catches

begin
file = open(“/tmp/some_file”, “w”)
# … write to the file …
rescue

# ex: rescue Timeout::Error, Errno::ECONNREFUSED

# or  rescue Exception => exception
# … handle the exceptions …
ensure
file.close # … and this always happens.
end

How to pass in a ‘new’ DB setting through fields in RoR

So typically in RoR you pass one object from page to page (in my opinion you do). You are passing the parameters to create “one” new object, etc.
Now what happens when you want to pass in two objects?
You can re-create it by passing in parameters which will re-create and mimic the “one object” way…
It’s a little contorted…
Here’s an example:

<%= hidden_field 'filter', "id", :value => filter.id %>
This generates filter[id] = value yeesh!
To use it:

How to use an “interior” field to specify another DB entry

<%= text_field 'filter_assignment', 'parameter1_setting', "size" => 10, :value => 61 %>

now in the “receiving” model of the post…

newFilterLink = FilterAssignment.new(params[:filter_assignment])

so we setup and used a new “db” entry by generating its parameters. This is just barely nuts, but works. A little bit contorted, as mentioned. Good luck!

How to use hidden values

So…say you’re in RoR and you want to pass to yourself some specific value to a hidden part of a form? Something that doesn’t totally match a DB entry, in the name?

Here’s how:

(This shows you how to specify the starting value, which also applies to other types besides hidden fields, BTW)

<code>

<%= hidden_field ‘filter’, “id”, :value => “newvalue” %>

</code>
This generates filter[id] = newvalue in the HTML.

What this actually does is allows you to setup an “arbitrary” value to pass into the controller.  Typically with RoR

<code>

<%= hidden_field ‘filter_to_use’, “id” %>

</code>

Generates <code>filter_to_use[id] = filter_to_use.id </code> in the HTML (I think). This is of course not useful if filter_to_use is a variable name that is just a variable you want to pass in to the next controller (i.e. it doesn’t exist).  So you need to use the <code> :value => whatever </code>above.

To use it in the receiving controller:

@filter = Filter.find(params[:filter_to_use][:id]) # retarded
What this does is looks up your parameter. So that’s a little contorted, but works, once you know how to set the value.

Roger's meanderings, notes to himself, bug reports, and other things