ruby

Sort array returned by activerecord by date (or any other column)

How can I sort an array returned by an activerecord query by a 'created_at' date column? Keep in mind, this is once the query has been executed. Don't tell me to do it in the query because I need this to happen in the view. Thanks ...

Best way to express "less than an hour in the future" in Rails?

If a train leaves less than an hour from now, I want its row in the schedule table to be highlighted red. Currently I'm doing the calculation like this: if Time.zone.now + 1.hour > t[from_station] # do whatever end This works and kind of makes sense, but I wonder if there's a clearer / more idiomatic way to express this (I could ima...

Recommendation for handling RoR App with Subdomains

I'm currently evaluating options for adding sub-domain support to a new Ruby on Rails app and was wondering if there were any suggestions or experience that others could share. Some possible Choices SubDomain-Fu http://github.com/mbleigh/subdomain-fu/tree/master Rolling your own http://www.robbyonrails.com/articles/2009/01/11/subdo...

Something like Psyco library (Python) for Ruby, exists?

I need more performance in a part of a program, coded in Ruby. In Python i could use Psyco library (that compiles the code, or a part before the execution) to improve the perfromance, but i dont know if exists something like that in Ruby. Thanks! ...

Simple Table Relation

Consider this: class User < ActiveRecord::Base # name, email, password end class Article < ActiveRecord::Base # user_id, title, text end class Favorites < ActiveRecord::Base # user_id, article_id end How do I put this all together in a way that I can have @user.articles (articles created by the user) and @user.favorite_article...

Keep getting OAuth::Unauthorized error when using oauth and twitter ruby gems

I am using the ruby twitter gem and oauth to gain access to users twitter accounts. In my code, I have: unless @user.twitter_authd? oauth = Twitter::OAuth.new('token', 'secret') session[:twitter_request_token] = oauth.request_token.token session[:twitter_request_secret] = oauth.request_token.secret @twitter_auth...

Ruby to python one-liner conversion

I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.: def copyright_dates(start_year = Date.today().year) [start_year, Date.today().year].sort.uniq.join(" - ") end I'm moving the app over to Django, and while I love it, I miss a bit of the conciseness. The same method i...

How can I show the git branch of a rails app within that app?

I've got a couple of different branches of a rails app running in a development environment on the same server (on separate databases, obv), and I'd like to make it very clear what branch is being shown when I hit the app through the web. Any ideas, short of running git-branch or git-symbolic-ref HEAD in backticks and parsing the output...

Acessing Mac applications from Ruby or PHP or Cocoa

I would like to access a couple of different Mac OS X applications from preferably Ruby, but I would settle for PHP. The applications are Elgato's turbo.264 and Apple's iTunes. Both have Applescript Libraries defined that would allow me to do what I want to do from Applescript, but I don't want to do this in Applescript. If I can't do th...

What's the Ruby equivalent of Python's os.walk?

Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's os.walk. The closest module I've found is Find but requires some extra work to do the traversal. The Python code looks like the following: for root, dirs, files in os.walk('.'...

Any suggestions for a ruby solution for populating a sqlite database from a mysql source

I have a Rails application that uses MySQL. The application needs to populate sqlite databases to be distributed to client applications from the master mysql database. The schema for the sqlite database will be very similar to the mysql database, but not identical. The sqlite databases will be populated with a subset of the schema and ...

Is it wrong to not implement all REST actions in Rails controller?

Let's say I have a SessionsController, which controls user login and logout, but the only actions I really need are new (for displaying login form), create (for authentication and login) and destroy for logging out the user. Is there any problem if I just have these three actions in my controller, or do I have to implement them all to m...

Running irb in emacs (via run-ruby) echos everything I type.

I'm running Windows Vista and Emacs 23.1.1 and I have installed Ruby using the "One Click Ruby Installer". I then installed the Emacs Lisp files that were installed with Ruby as specified in inf-ruby.el. When I run the run-ruby (M-x run-ruby) function, irb starts but every time I press Enter, irb prints out the line I just typed. For ...

problems with jruby version of ruby-debug

Whenever I use 'n' to step over a line it steps into it instead. This happens to me on all the versions of jruby i've tried, the latest being 1.3.1 Does it work right for anyone? If so any idea how to fix it, because it's too painful when it steps into every function every time. Also, I use it like this if it makes any difference. re...

ruby inheritance vs mixins

In Ruby, since you can include multiple mixins but only extend one class, it seems like mixins would be preferred over inheritance. My question: if you're writing code which must be extended/included to be useful, why would you ever make it a class? Or put another way, why wouldn't you always make it a module? I can only think of one r...

render_to_string stripping markup

I am using render_to_string within a JSON 'render' response. The render_to_string method seems to be striping my HTML, what am I doing wrong here? Here's a sample: render :json => {:html => render_to_string(:partial => 'view', :locals => { data => @data} )} The response is coming through without any markup on it. ...

Best Practices for receiving email in rails

Hey Everyone, I've been trying to figure out the best way to handle incoming email in a rails applications. I realize "best practices" is quite subjective, so I'll start by stating that my primary concerns are scalability and efficiency. This is an issue primarily because my use will involve handling potentially large attachments. Se...

Rails Polymorphic relationship and link_to

Here's my Schema class Menu < ActiveRecord::Base belongs_to :menuable, :polymorphic => true end class Page < ActiveRecord::Base has_one :menu, :as => :menuable end class Links < ActiveRecord::Base has_one :menu, :as => :menuable end I want to link to a polymorphic class in the Menu view using link_to, e.g. <%= link_to menu.na...

Existence of right addition/multiplication in ruby?

I've seen how to overload + and * in Ruby, so that my_obj + other calls my_obj.+(other). In Python, you do this with __add__, and there's a corresponding __radd__ for overloading other + my_obj. Is there really no equivalent right-sided addition/multiplication in Ruby, and does that make it necessary to redefine + for each potential clas...

In TCPServer (Ruby) how can i get the IP/MAC from the client?

Hi, i want to get the IP Address of the client in a TCPServer in Ruby. And (if it is possible) the MAC Address. For example, a Time Server in Ruby, see the comment. tcpserver = TCPServer.new("", 80) if tcpserver puts "Listening" loop do socket = tcpserver.accept if socket Thread.new do puts "Connected from" + # HERE! How c...