ruby

Rails 3 and Factory Girl creating user only if attributes are passed in

Hey, I'm using Rails 3 rc, Factory Girl, and Rspec, and Authlogic. Is there any way or reason why this would happen: When I create a user like this: @user = Factory(:user) I get an issue with password confirmation being "too short". my factories.rb is Factory.define :user do |u| u.username "Test User" u.email "...

how to pass multiple parameters to rake task.

I want to pass multiple parameter but don't know the numbers. Like model names. how to pass those parameters into rake task and how to access those parameters inside rake task. Like, rake test_rake_task[par1,par2,par3] ...

Getting client IP from Ruby script through xinetd

How can I get the client IP Address from inside a Ruby script that is launched by xinetd through a stream socket? In PHP I would just use stream_socket_get_name(STDIN, true); Thanks in advance! ...

Distributed sequential random number generation in Ruby 1.9.2

The Random class in Ruby 1.9.2 is guaranteed to generate random numbers in the same order, given a particular seed and range. For instance: r = Random.new(23) r.rand(100) # 83 r.rand(100) # 40 But suppose I want to generate the next number in the sequence on another computer (without re-generating the earlier numbers i...

How to interleave arrays of different length in Ruby

If I want to interleave a set of arrays in Ruby, and each array was the same length, we could do so as: a.zip(b).zip(c).flatten However, how do we solve this problem if the arrays can be different sizes? We could do something like: def interleave(*args) raise 'No arrays to interleave' if args.empty? max_length = args.inject(0) {...

inserting blocks of text in haml

In my Jekyll blog I use the include tag to put the contents of a file into the document. However if I attempt to do this with a HAML based document the indentation of the included text is wrong. :preserve does not work because it requires indentation. Is there a way to specify a block of text without depending on indentation? %html %b...

How do I use Fixtures.identify?

What are the parameters and what is the significance of them? ...

When to call a "require" in Rails?

I have a more conceptual question in Rails... or Ruby for that matter: Is it best to call a require right before the method that needs it, group my requires at the beginning of the class or somewhere in an initializer when Rails boots? Does it matter from a performance point of view? From a readability point of view? Does it make a dif...

Overridable Active Record Fields

I have a cms and public facing website built in rails that pulls in an XML file (from another system) every hour that contains all our product data. But once in a while, the marketing team might want to override certain fields in order to display them differently on the website. For example, they may want to alter the name of a product...

How can I force Rails to synchronize transactions to a MySQL table across connections?

I have a high-traffic site that records web hits in a table, Hits. There's logic in place so that duplicate hits are discarded (where the definition of what defines a hit as duplicate is arbitrary for the purposes of this example). Problem: with multiple web servers running (against the same DB), two or more hits can arrive at the same ...

Inherit active_record in rails.

Right now my classes are look like this. class BalanceName < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end class Balance < ActiveRecord def before_validation set_blank_attributes_to_nil(@attributes) end end I want to inherite activer record into one class and than want...

ruby rails functional test problem

I'm getting the following problem with my functional test. mrbernz:mylife bernardleung$ ruby test/functional/forums_controller_test.rb ..... 1) Error: test_should_create_forum(ForumsControllerTest): ActiveRecord::StatementInvalid: Mysql::BadFieldError: Unknown column 'id:3 name' in 'field list': INSERT INTO roles (id, id:3 name) VALU...

phonegap android sample project not building

I'm trying to follow the setup instructions on phonegap-android's wiki page, but I'm having trouble. I've got all of the prereqs/path variables/etc set-up, but when I execute the command (in gitbash as per the instructions) for building the project, I get errors which I do not understand. Here is the script which I run: $ ruby ./droid...

Get repository link from gem?

Is it possible to get the git repository of a gem? Eg the gem thor has repo of http://github.com/wycats/thor.git Could I get this repo link from the thor gem? ...

Ruby Equivalent

I am in the process of implementing a Java library in Ruby. I have come across the following road block. Is it possible to implement the following code in ruby? Are there any ruby equivalents for byte[], IvParameterSpec, SecretKeySpec ? private String decrypt(String token) { //parse token into its IV and token components byte[] i...

Getting a return value out of Multiple Blocks With Ruby

require 'net/http'; require 'uri'; require 'rexml/document'; require 'sqlite3'; def download_torrent(episode_id, torrent_url, limit = 10) # Check to make sure we haven't been trapped in an infinite loop if limit == 0 then puts "Too much redirection, skipping #{episode_id}"; return true; else # Convert the URL of the ...

How to use :conditions to compare (a datetime) with (datetime + 'number of days') ?

I'm building an application that checks when a user needs to change the oil of one of his cars. Here's how it works: The user enters the name of his car (e.g. 'Ford Mustang') The user enters the time the car will run on one oil change (e.g. 3.months) The user saves the car to the database Repeat above process for all his other cars. (Y...

Ruby global match regexp?

In other languages regexp you can use //g for a global match. However, in Ruby: "hello hello".match /(hello)/ Only captures one hello How do I capture all hello? ...

How do you find out what controller/action you are in via ruby code?

I have a different subheader partial I want to render dependent on where I'm at in my application. How do I go about determining where I'm at via ruby? Or do I need to parse the URL? Example : If I'm at my root level I want to use /home/subheader, if I'm in controller 'test' I want to render /test/subheader, etc... etc... basically...

trying to POST with ruby mechanize

I've captured the login HTTP headers using firefox plugin LiveHTTPheaders. I've found the following url and variables. POST /login email=myemail%40gmail.com&password=something&remember=1&loginSubmit=Login And here's the code I am running: require 'rubygems' require 'mechanize' browser = Mechanize.new browser.post('http://www.mysit...