ruby

Ruby efficient way of building an array from an array of arrays

I have an array of ActiveRecord objects, each one which has its own respective errors array. I want to flatten it all out and get only the unique values into one array. So the top level array might look like: foo0 = Foo.new foo1 = Foo.new foo2 = Foo.new foo3 = Foo.new arr = [foo0, foo1, foo2, foo3] Each one of those objects could pot...

Any way to determine which object called a method? [Ruby]

I'm hoping that Ruby's message-passing infrastructure means there's some clever trick for this... How do I determine the calling object -- which object called the method I'm currently in? ...

ROR heroku PostGres issue

getting error: ActiveRecord::StatementInvalid (PGError: ERROR: argument of HAVING must be type boolean, not type timestamp without time zone controller code snippet: def inactive @number_days = params[:days].to_i || 90 @clients = Client.find(:all, :include => :appointments, :conditions => ["clients.user_id = ? A...

One-liner to Convert Nested Hashes into dot-separated Strings in Ruby?

What's the simplest method to convert YAML to dot-separated strings in Ruby? So this: root: child_a: Hello child_b: nested_child_a: Nesting nested_child_b: Nesting Again child_c: K To this: { "ROOT.CHILD_A" => "Hello", "ROOT.CHILD_B.NESTED_CHILD_A" => "Nesting", "ROOT.CHILD_B.NESTED_CHILD_B" => "Nesting Again", ...

Building a rails form to filter an index page?

G'day guys, I'm having an issue with filtering the presentation of several thousand trade items I have in my system. As per the specs of the system we're building we have to have a form that allows people to put in a start date and then an interval in minutes, to filter the presentation of the items. I've built my helper functions to ret...

Unicode filenames on windows in ruby

I have a piece of code that looks like this: Dir.new(path).each do |entry| puts entry end The problem comes when I have a file named こんにちは世界.txt in the directory that I list. On a Windows 7 machine I get the output: ???????.txt From googling around, properly reading this filename on windows seems to be an impossible task. Any s...

Creating a specialised view filtering form in Rails

G'day guys, I have a current set of data, and I generate multiple analyses of this data (each analysis into its own active record item called a pricing_interval) using a helper function at the moment. Currently to analyse the set of data, you need a start time(using datetime_select) an integer (using text_field) and a name (using text_f...

~/.irbrc not executed when starting irb or script/console

Here's what I've tried: 1. gem install awesome_print 2. echo "require 'ap'" >> ~/.irbrc 3. chmod u+x ~/.irbrc 4. script/console 5. ap { :test => 'value' } Result: NameError: undefined local variable or method `ap' for # ...

how to get audio frequency data from a wave file?

I want to build a speech recognition engine in ruby. I know i'll never get there, doing it just for fun. I need to get data for the frequencies of the sound stored in a wav file to compare with data i already have of different sounds that i want to recognize. I will write the code in ruby but i dont think there are any libraries for thi...

Passing data between blocks using sinatra

Hi All I'm trying to pass data between blocks using sinatra. For example: @data = Hash.new post "/" do @data[:test] = params.fetch("test").to_s redirect "/tmp" end get "/tmp" do puts @data[:test] end However whenever i get to the tmp block @data is nil and throws an error. Why is that? ...

devise roles: different roles, in some cases same rights

I have two roles in devise. The first is the admin and the second the normal user-role. Now I´d like to give these two groups in some cases same rights with before filters. How does this work? I have: before_filter :authenticate_user!, :only => [:new, :create] before_filter :authenticate_admin!, :only => [:new, :create, :edit, :update...

Rails + Dragonfly gem: Saving image in a directory structure based on ActiveRecord object attributes

I'm using dragonfly gem to manage images and attachments in my rails app and I need to store images in a specific directory structure based on my user model. let' say I have user model which has a name and each user has many albums, which have a name also, then I want the images to be stored in "#{RAILS_ROOT}/public/system/#{user.name}/...

Best way to get the values of a hash ordered by their respective keys? (Ruby)

Here's what I'm doing atm: test = { 'd' => 20, 'b' => 40, 'c' => 30, 'a' => 50, 'e' => 10 } f = [] test.to_a.sort.each do |e| f << e[1] end puts f.join(' ') Outputs: 50 40 30 20 10 Is there a more efficient/concise/better way to do this? And before someone says so, no, I can't use an array. :p EDIT Sorry, posted the w...

Is there a Cart/Catalog app out there that isnt a full site?

Every solution i come across seems to be a complete store with far too many options and too much functionality. I can write my own cart to get the minimal functionality i need out of the frontend, im just trying to cut out the time writing a complex backend. What im really looking for is something that offers the backend functionality ...

Ruby on Rails: controlling the layout of an error message in ActiveRecord

My error messages in Rails look like the following: "Email Your email is invalid." Why is the name of the field prefixed within the string itself? It makes the error messages look awfully odd. Is there anyway to circumvent this behavior so that I can just see "Your email is invalid." ...

rails search a category for sub categories

Hello, I am using the http://github.com/collectiveidea/awesome_nested_set awesome nested set plugin and currently, if I choose a sub category as my category_id for an item, I can not search by its parent. Category.parent Category.Child I choose Category.child as the category that my item is in. So now my item has category_id ...

ruby c extensions: character values over 127

I am trying to make a C extension for Ruby that includes a method returning a string, which will sometimes have character values that need to be in an unsigned char. In http://github.com/shyouhei/ruby/blob/trunk/README.EXT, all of the functions listed for turning C strings into Ruby strings take signed chars. So I couldn't do this: unsi...

Getting ruby to wait for return from command line call

I'm using the backtick method to make an ImageMagick call on each file in a directory. It looks like 10 or so images get processed and then it freezes. Forgive my ignorance but is Ruby trying to make all these calls serially without waiting for a return? How should I be doing this? I would just use ImageMagick to directly batch process ...

How do I temporarily change the require path in Ruby ($:)?

I'm doing some trickery with a bunch of Rake tasks for a complex project, gradually refactoring away some of the complexity in chunks at a time. This has exposed the bizarre web of dependencies left behind by the previous project maintainer. What I'd like to be able to do is to add a specific path in the project to require's list of pat...

migrating simple rails database to mysql

i am interested in creating a rails app with a mysql database. i am new to rails and am just trying to start creating something simple: rails -d mysql MyMoviesSQL cd MyMoviesSQL script/generate scaffold Movies title:string rating:integer rake db:migrate i am seeing the following error: rake aborted! NoMethodError: undefined method `o...