ruby

Rails Model Associations for Item-Item Relationship?

Looking for some guidance on the best way to implement this scenario: I have an items table (of products) and want to support the ability to cross-sell / up-sell / complement items. So there is an item-to-item(s) relationship here. In this join table I need to include additional attributes beyond the keys such as the sales_relation betw...

Proper way to parse this line with FasterCSV?

I have the following line in a CSV file that's giving me issues when parsing: 312,'997639',' 2','John, Doe. "J.D." ',' ','2000 ',' ','Street ','City ','NY','99999','','2010-02-17 19:12:04','2010-02-17 19:12:04'; I'm parsing with the following par...

Writing better Ruby: How to cast a objects to false or true?

In ruby, what is the best/most-elegant way to return a value such as: #method returns true or false if 'do i match' is present in string def method(str) str =~ /do i match/ end ...

Why doesn't each_slice work?

I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found. I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0] API: http://ruby-doc.org/core/classes/Enumerable.html#M003142 Example: (1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `eac...

Timed Page Caching in Rails

In a project I'm working on, I'm requesting data from an external API in my controller, which is then displayed in the view. I've recently begun running into exceptions being thrown due to rate limits, which are caused from excessive calls to the API. To fix this, I'm assuming that I need to implement some sort of caching system. I've be...

Algorithm for deviations

Hi! I have to track if given a week full of data integers ( 40, 30, 25, 55, 5, 40, etc ) raise an alert when the deviation from the norm happens (the '5' in the above case). An extra nice thing to have would be to actually learn if 5 is a normal event for that day of the week. Do you know an implementation in ruby that is meant for th...

Editing records with SQLite, DataMapper, and Sinatra.

I'm in the process of learning Sinatra and DataMapper. To do so, I've been playing with a "customer database" project. Creating and deleting records is trivial and now I'm working on editing records. So far I've managed to piece together a form in my views and a couple of routes that I thought would edit a record. Here's some code ...

gems: difference between sqlite3 and sqlite3-ruby?

I accidentally ran: sudo gem install sqlite3 instead of: sudo gem install sqlite3-ruby So now when I run gem list I get: gem list *** LOCAL GEMS *** sqlite3 (0.0.7) sqlite3-ruby (1.2.5) What is the difference between the two? And do I need both? ...

"sqlite3 not found" error in ruby on rails

I am very new to RoR... I installed Ruby and installed its gems... then downloaded and installed MySql... created my first directory demo. then started the server using ruby script/server entered the http://localhost:3000 url in the browser and get a "welcome aboard" page..all is well till now... now I create a controller using ruby...

Why is "wsdl" namespace interjected into action name when using savon for ruby soap communication?

I'm trying to access a SOAP service i don't control. One of the actions is called ProcessMessage. I follow example and generate a SOAP request, but I get an error back saying that the action doesn't exist. I traced the problem to the way the body of the envelope is generated. <env:Envelope ... "> <env:Header> <wsse:Security...

I think my rails gem is broken.

So I'm working on one computer with some ruby/rails code and its working fine. On another computer however I'm having a problem when trying to create a model or run the server and it looks like the same problem. look at this output: $>script/generate /usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks...

Text editor(FCKEditor) ruby on rails/ PHP

I have an application that is running on Rials so now I want to add the text editor(FCKEditor/TinyMCE) to adit text/uploading images on website. I have google the internet until I got FCKeditor for Rails(http://rubyforge.org/projects/fckeditorp/) "which has a function of uploading pictures but ones you uploaded the images you can't dele...

Libxml Ruby : Do I need to manually garbage collect node sets returned by doucment#find?

The documentation for LibXML::XML::Document#find mentions that following code style needs to be used to avoid seg faults: nodes = doc.find('/header') nodes.each do |node| ... do stuff ... end Is this all I need to do? Below the example code box there is some commented out code: # nodes = nil # GC.start Do I need to include this c...

How to escape slashes in single-quote-like string?

ruby 1.8.6 (2007-09-24 patchlevel 111) str = '\&123' puts "abc".gsub("b", str) => ab123c puts "abc".gsub("b", "#{str}") => ab123c puts "abc".gsub("b", str.to_s) => ab123c puts "abc".gsub("b", '\&123') => ab123c puts "abc".gsub("b", "\&123") => a&123c <--- This I want to achieve using temporary variable If I change str = '\&123' to str...

Rails 2.3 caching by time

Hi guy, I would like to cache my fragment page in my rails application by time. I found this plugin to do this => ici but any download is available. I searched in the rails doc but I don't found how to cache my fragment by time. Are you know another plugin to do this or another method to do this ? Thanks. ...

How do you access an instance variable within a mixin method?

Hi, How do you access an instance variable within a mixin method? I can think of 2 ways, but both seem problematic. Have the mixin method access the instance variable directly as any class method would, e.g self.text. Problem with this is that it places restrictions on where the mixin method can be used, and forces the class doing the ...

Where does the Rails initializer go in a gem?

I'm trying to extract some functionality from my existing app into a gem. The existing functionality uses an initializer to load up a config file when Rails starts up... config/initalizers/myinitializer.rb Where should this initializer go in the gem? Do I mirror the path structure inside the gem or put it somewhere else? This will be ...

Why doesn't RoR take advantage of native extensions/code inlining techniques?

Those outside of the ruby community (and some inside) oft reference ruby applications as being slow. For many of us this is an irrelevance, IO bound operations etc. However, when it does become a problem there is little to hold us back from taking advantage of native code to speed things up. To this end I am wondering why RoR (itself the...

IronRuby: Cannot call method on a COM Object with one or more arguments.

When I try and call any method on a COM Object that takes one or more arguments, I get the following error on the last argument: Could not convert argument 0 for call to Open. (ArgumentError) Everything works fine when calling a method that takes no arguments, or getting/setting a property. Here is the code that gives me the error abo...

In Ruby how do I generate a long string of repeated text?

What is the best way to generate a long string quickly in ruby? This works, but is very slow: str = "" length = 100000 (1..length).each {|i| str += "0"} I've also noticed that creating a string of a decent length and then appending that to an existing string up to the desired length works much faster: str = "" incrementor = "" lengt...