ruby

Include files in a command line with Ruby

When running ruby scripts as such ruby some-script.rb How would I include a file (e.g. configuration file) in it dynamically? ...

Getting output of system() calls in ruby

If I call a command using system() in ruby, how do I get it's output? e.g. system("ls") ...

SHA1 hashes not matching between my Rails and Cocoa apps

I have a Cocoa app send some data along with a SHA1 hash of that data to a Rails app which verifies that the data and the hash match, but they do not. To be sure I have logged hex versions of the data that is hashed to the console at both the Rails and Cocoa sides, and they match exactly. Here's the Cocoa part: #import <CommonCrypto/C...

Rails STI Controllers

I have a Rails site using STI with the following classes: Pages Homepage < Pages LandingPage < Pages On the front-end all requests get handled by the Pages controller. However, if the object detected is actually an instance of LandingPage, i'd like to have the action on a LandingPages controller get called. (for example, the show me...

Windows Rails-IDE with Remote-File-Support (FTP/sFTP)

I am currently using Aptana RadRails for Ruby on Rails development on my local system. Now I want to work on a Rails-Application that is hosted on my dedicated server, but unfortunatly RadRails does not provide support for Remote File Access (apart from SVN). Is there an IDE for Rails-Applications that gives me the ability to work on an...

ActiveRecord: Can I copy associations?

Is there a way to copy the associations of one model to another... template_model = MyModel.find(id) new_model = template_model.clone new_model.children << template_model.children # I want to *copy* children ...such that I copy the children from the template to the new model? (In fact this code moves children from the template to the ...

Rails "validates_uniqueness_of" Case Sensitivity

Here is the model (I am using SQLLite3): class School < ActiveRecord::Base validates_uniqueness_of :name end For example, after I add "Yale", I cannot add "Yale" but can add "yale." How can I make the validation case insensitive? EDIT: Found it - Active Record Validations ...

Ruby arrays: %w vs %W

What is the difference? ...

Cut off the filename and extension of a given string.

I build a little script that parses a directory for files of a given filetype and stores the location (including the filename) in an array. This look like this: def getFiles(directory) arr = Dir[directory + '/**/*.plt'] arr.each do |k| puts "#{k}" end end The output is the path and the files. But I want only the path. Inste...

Rails Application Admin Section

I am working on my first Rails application and want to create an admin section. Do I want to keep my views and controllers completely separate (that is, in separate directories) for the admin section and the rest of the site? How can I organize my views/controllers in custom directories (how do I configure the routing)? ...

How to parse substring between last set of parentheses in string in ruby.

In my ruby on rails app, I am trying to build a parser to extract some metadata out of a string. Let's say the sample string is: The quick red fox (frank,10) jumped over the lazy brown dog (ralph, 20). I want to extract the substring out of the last occurence of the ( ). So, I want to get "ralph, 20" no matter how many ( ) are ...

Save has_and_belongs_to_many child

I have a User model and a Role model. They are joined by a has_and_belongs_to_many relationship. When an admin creates a user I want them to be able to assign a role to the user and have it saved when I call @user.save The thing is though is that I get a warning that I can't mass-assign the roles relationship. Any suggestions on how to...

rails: how to html encode/escape a string? is there a built-in?

Yay, silly question time. So I have an untrusted string that I simply want to show as text in an html page. All I need to do is escape the chars '<' and '&' as html entities. The less fuss the better. I'm using utf8 and don't need no other stinking entities for accented letters and so on. Is there anything built-in in ruby or rails, ...

Rails ActiveRecord Relationships

How do the relationships magically function when only the models are altered? If I want a "has__and___belongs___to__many" relationship, what should I name the table (so Rails can use it) that contains the two foreign keys? ...

How do I use redirect_to if I've got multiple controllers in different subdirectories?

I've created a small application to learn RoR. (Book database) It consists of a read-only area and a read-write admin area. After I've got the admin functionality working first, I've moved the controller into a subdirectory and created the read-only controller. Now when I'm updating a book in the admin area, the redirect_to function re...

Rails named_scope with has_and_belongs_to_many

Hi, i have 3 tables - films, films_genres (for connect 2 tables) and genres. In Model film.rb - has_and_belongs_to_many :genres In Model genre.rb - has_and_belongs_to_many :films So, how I can write this sql code: SELECT * FROM genres INNER JOIN films_genres ON genres.id = films_genres.genre_id WHERE (films_genres.film_id = 1 ) with n...

Ruby XMLParsing Exception

I get a ParseException every time I try to parse a http get_response data in Ruby. The Exception is because of the presence of '&' in the data. How do I solve this? Illegal character '&' in raw string (REXML::ParseException) ...

Calling a file from a website in Ruby

Hi guys, I'm trying to call resources (images, for example.) from my website to avoid constant updates. Thus far, I've tried just using this: @sprite.bitmap = Bitmap.new("http://www.minscandboo.com/minscgame/001-Title01.jpg") But, this just gives "File not found error". What is the correct method for achieving this? ...

Rails Migration

My migration is as follows: class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :email t.string :password t.string :name t.boolean :male t.boolean :admin t.timestamps end end def self.down drop_table :users end end When I go to script/con...

how (replace|create) an enum field on rails 2.0 migrations?

I would like to create an enum field at sone migration I'm doing, I tried searching in google but I can't find the way to do it in the migration the only thing I found was t.column :status, :enum, :limit => [:accepted, :cancelled, :pending] but looks like the above code runs only on rails 1.xxx and since I'm running rails 2.0 this...