ruby

Rails class name/type not working for a polymorphic has_many :through

I have an invoicing system that manages debits and credits. Basically the invoice amount is obtained by the sum of its debits and the balance is derived by taking the sum of its credits and subtracting it against the total amount. I'm doing this with four models. Invoice Line Item Debit Credit The way it works is via a join model (L...

How to modify the create & update method in RoR?

I know that the after the create or update methods is done, they have the method like this respond_to do |format| format.js end Since I change my form from remote form to non remote form, so I won't use the format.js anymore, I just want to refresh the page, after the user create/update a product, so I have this code: respond_to ...

How do I hmtl_escape text data in a sinatra app?

I have a small Sinatra app which generates html fragments for me from an ERB template. How do I html_escape the output? The <%=h somestring %> helper does not exist in Sinatra. ...

How to set routes.rb for ONLY controller and view object?

I have a controller called store_controller, and views for store. But the store doesn't have the model, but I want to use store_path in the code. How can I add the store_path in routes.rb? ...

urls from gsub not correct

i have some problems when i try to check urls after get them from gsub method. ex from the console: ('http://ale.it' =~ URI::regexp).nil?.to_s => "false" and it works fine but if i launch this: "http://ale.it".gsub(/http[s]?:\/\/[^\s]+/, ('\0' =~ URI::regexp).nil?.to_s) => "true" it doesn't work. How can i do, for get correct...

Mongodb: What to know before using?

I'm starting a hobby (non-revenue) project using Ruby on Rails. I've done a fair amount of development in Rails using Postgresql, and I can make a pretty good imitation of normalized schema. However, Mongrodb looks shiny and new. What better for trying out something new than a hobby project? Think back to when you started using Mongo...

how to convert a hash into an array with particular order? (ruby)

I have a hash of hashes (@post) and I want to save it into csv. Strange for me is that my approach works for the header but not for the rows. If I display the rows using csv << (@post_csv_order.each {|element| puts single_post[element]}) I can see the right strings on the screen but the csv files contains values of the hash key not t...

Does ruby have a list type that keeps contents sorted as adds/deletes occur?

I need a datastructure in Ruby that keeps its elements sorted as elements are added or deleted and allows (at least) the ability to pop the first element off the list. The closest thing I've found in the ruby docs is SortedSet. However, this doesn't seem to provide any way to access the elements by their index (or even pop the first el...

Right clicking with a Webkit view

I'm working on a project in Ruby right now that is essentially a web-app. We like the format of web-apps and some of the natural agile advantages we have building for the web. However, we want to be able to package our application and distribute it in a standalone format. Ideally, we would like to essentially make a .app package for Ma...

ActiveRecord has_many where two columns in table A are primary keys in table B

I have a model, Couple, which has two columns, first_person_id and second_person_id and another model, Person, whose primary key is person_id and has the column name Here's the usage I want: #including 'Person' model for eager loading, this is crucial for me c = Couple.find(:all, :include => :persons)[0] puts "#{c.first_person.name} an...

Ruby: catching access to variable inside class

I have a class, which de-constructs incoming string into a nested array cascade. For example for an input abcde it will produce a [[[[a,b],c],d],e] array. Just now, if I access to set any top level value of cascade, the []=(index, value) method of my class will be invoked. But I also need to catch the access to the nested array within c...

Configuring Postgres in Ubuntu 9.1 Rails dev-box for Cucumber testing...

I'm trying to install Postgres 8.4 for a Rails dev-box and I'm having a couple problems. I installed postgres and pg-admin3 through apt-get. Using the lastest Rails 2.3.5 and lastest Ruby 1.9.1 Now the configuration is bothering me. I found some documentation regarding setting up the user for the postgres user(which is the default adm...

Regex that expresses "at least one non-digit"

I want to validate usernames according to this schema: Allowable characters: letters, numbers, hyphen, underscore First character must be a letter or a number The username cannot be all numbers This regular expression satisfies 1 and 2 above, but I can't figure out how to satisfy 3: /^[a-zA-Z\d][\w\-]+$/ (I'm using Ruby, if that's...

onComplete for Ajax.Request buggy with cached content

I have a Ajax.Request in which I use onLoading and onComplete to show and hide a load animation gif. The problem is that every 10 clicks or so, the load animation fails to hide and just stays there animating even though the ajax request has returned successfully. I have a number of div elements that each has its own respective load anim...

Jruby Gem For Heroku

I am trying to deploy a Jruby app on Heroku. Unfortunately, when I try to install gem for Heroku using jruby, I get following error: /proj/jruby-1.3.1/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError) from /proj/jruby-1.3.1/lib/ruby/1.8/mkmf.rb:1:in `requir...

Prototype Toggle through Array Only Toggling Last Item

I'm a beginner and my brother has showed me some prototype. All the weeks successfully toggle the last week in the array but no other. Please let me know if I need to be more specific! Thanks! <%= @athletic_program.program_name %> <br> <br> <% array of_ids = Array.new %> <% @program_weeks.each do |program_week| %> <% array_of_ids...

Order items in MongoDB according to the size of an array with MongoMapper?

I'd like to select a collection of items ordered based on the number of items within an array. Hopefully the following example will clarify my rather poor explanation: class Thing include MongoMapper::Document key :name, String key :tags, Array end I'd like to retrieve all Things ordered from those with the most tags to those w...

hash assignment when (key => value) are stored in an array? (ruby)

I have hash (@post) of hashes where I want to keep the order of the hash's keys in the array (@post_csv_order) and also want to keep the relationship key => value in the array. I don't know the final number of both @post hashes and key => value elements in the array. I don't know how to assign the hash in a loop for all elements in th...

how to read nokogiri.org documentation? (ruby+mechanize+nokogiri)

last week I started to write a script in ruby. I needed to scrape some data from the web so I was recommended to use mechanize and then nokogiri. Mechanize documentation says Mechanize uses nokogiri to parse html. What does this mean for you? You can treat a mechanize page like an nokogiri object. After you have used Mechanize to navig...

Is there timezone offset bug in ruby on rails?

I'm in Korea. Rails version is 2.3.5 in my environment, config.time_zone = 'Seoul' config.active_record.default_timezone = 'Seoul' created_at and updated_at value is inserted with local time in database. but, when I access model.created_at or model.updated_at, the value is understanding with UTC +9:00 So, I insert model to da...