Category Archives : Ruby on Rails

Backbone.js + Rails 2

You all know that my opinion of Ruby On Rails couldn’t be any lower if it was under the basement carpet of a Pompeii1 house, so this post won’t be very suprising. So, awhile back I was having a discussion with a ¬†friend about how, as a general rule, about 90% of a web app should really be taking place on the browser, and this was vindicated in my opinion with several other developers coming to much the same conclusion and releasing some very interesting application frameworks for javascript Single Page Applications. Of course this conversation took place when jQuery was just really starting to take off. The problem at that time was that no one was really talking about a client side application in javascript methodology, how such a thing could or would work. In hindsight it all seems obvious, but actually, Backbone and other such frameworks represent some really great out of the box creativity. Anyway, after my last Rails project, I more or less swore I would never work with that ass backwards platform ever again. I decided to revisit this idea of Single Page Applications and low and behold I stumble onto Backbone.js, and I decide I am going to learn to use this tool. While looking around for some books on the topics of jQuery and Backbone (I’ve more or less decided to give up the ghost and use jQuery ui elements in all future applications cause rolling your own is just too much work.) low an behold I find […]


Rails will_paginate links to wrong page 4

Well, While setting up a website today I found a bit of retardation in will_paginate. Aside from poor and complicated documentation and an incredible amount of bloat for what amounts to a very simple kind of website component, I found that will_paginate was linking to /?page= on a page that showed in the url bar as /products, so the link should have been /products?page=. So I looked around for a quick fix, and this is the best I got: Revisited: roll your own pagination links with will_paginate and Rails 3 OH MY GAWD. You have to be shitting me. That is hands down the most retarded thing I have ever seen. So, like the little hack monster that I am, I did it faster and easier with less code. If you want to change the pagination links, you can do it with jQuery. $(‘div.pagination a’).each(function () { var url = $(this).attr(‘href’); url = url.replace(‘/’,”); $(this).attr(‘href’,url); }); Why? Because ideally, will_paginate shouldn’t give a shit about the url, that’s not its job, that’s my job, its job is to ADD get parameters, and manage what those parameters should be, anything else is retarded.

Rails 3 – PHP Style Global Variables, $_SESSION, $_GET, $_POST in the models, and everywhere.

PHP Globals in Rails 3 So, as part of my slow descent into hell for even showing this stuff, I thought I would make a quick and dirty post about getting PHP like globals into your models in Rails. I wrote a longer article about globals in the model in rails which you can check out for some other options. def some_before_filter $_SESSION = session $_GET = request.GET $_POST = request.POST $GLOBALS = {} end Yeah, that simple. When it comes to globals, the ruby and rails community is kind of like the child of a former Nazi sympathizer who got off the hook at Nuremburg. Yeah, they’re related, but they just don’t want to have anything to do with them, and if you remind them of the connection they get really angry.

Never Again! Backing up your rails .sqlite3 dbs

Well, with mysql, it’s pretty hard to overwrite your dbs, but with sqlite, it’s real easy. I did this, luckily I only lost about a days worth of work, but nevertheless, I have instituted a new rule for sqlite3 dbs: Hourly backups. Here’s the script: #!/bin/sh D=`date +%H` cp -f /var/www/app/db/production.sqlite3 /var/www/fapp/db/production.sqlite3.bak.$D

Love Ruby, Hate Rails, but still use it — or Ruby vs PHP 11

Okay, that title was a bit extreme, I don’t really hate Rails. I don’t get why people feel the need to be so completely polarized about a framework, or a language. It’s either the best thing since sliced bread, or the worst, and nowhere in between. Still, most developers who aren’t Ruby purists, can tend to have a love hate relationship with Rails. Rails is easy to develop with, full stop. It’s practically impossible for you not to push out an app in AT LEAST 1/4 of the time it would take in straight up PHP. The problem is, PHP and Rails are apples and oranges, they aren’t the same thing, Rails is a framework built with the Ruby language, PHP is just a language. If you want a comparison, try CakePHP and Rails. Saying that Rails isn’t as fast as PHP is kind of mind boggling. Rails is slow, and so is ruby, and it will never be as fast as PHP or faster, period. Unless PHP becomes abandoned, which doesn’t seem likely. PHP is 100% geared towards a Server Side Scripting language, from the ground up, it’s written directly into the PHP parser to handle HTML with embedded PHP tags. Ruby is a general purpose scripting language with addon modules for parsing ruby code in HTML, like ERB. Regardless of this, you’d barely notice how much slower it is in reality because at this point, it’s very much a matter of hundredths of a second difference (probably). That barely even matters in large […]

Ruby on Rails Session in Models: Not So Evil Actually 3

I just finished reading this post from on how to put your session into your models. First off, I love this guy, and his site, and most of what he says, but I’ve had it up to here with this nonsense. I also kind of felt a little dissed by the PHP comment. I understand he was joking, and it was kind of funny, PHP programmers are NOTORIOUS for using $_SESSION to store EVERYTHING. At the same time, as Voorhaus says, all comedy is truth and pain, and the truth is that PHP programmers do that, and the pain is that many programmers coming to Ruby and Rails feel alienated by that community because they are so opinionated (And not always right). Rails is kind of Opinionated by design, and that’s okay, but more often than not, the Rails and Ruby community is uneccessarily sadistic in its treatment of contradictory opinions, they have a “my way or the highway” kind of speech when in fact, you can do it anyway you want, and all they are doing is looking like douche bags for treating you as a sub-human for having a contradictory opinion. As I see it, at the core of the above issue, sessions in the model, is global variables, and the irrational fear of them. Phear Globalz There is some practicality in not depending on global variables, especially when you are TALKING about programming. When you are doing it, it’s another story. They can theoretically be the source of problems. Yes I […]

SOAP APIs R Klunky or How to waste bandwidth for no good reason

The fact that SOAP is still around is, to be honest, absolutely beyond my comprehension. No serious effort seems to have been put into something like JSONRPC. Still, when it comes to some API calls, SOAP is pretty darn good, but this here is one example of it being used as just a waste. I recently setup a client, who has a Spree Commerce Ruby on Rails store, with the Ebay Trading API. Basically, he lists items on his store, sends them to ebay, and his store is notified when they have sold on ebay, and are thus removed from his store, or when they sell on his store, they are ended on ebay. Pretty standard setup, nothing special. The app subscribes to ebay trading api platform notifications which are sent to the server in a SOAP format. The SOAP notification that an item has sold is 11kb. Yeah, I know what you are thinking, that’s small. No, it’s not. When you are talking millions of users, it’s not small, it’s huge. The information that I actually need from that is 1.1k. To be quite honest, I need even less. Apparently the client notifications api sends out notifications in as REST kind of way, i.e. as a url with key=>value pairs. Really, all I need is: EventName=TheEvent&ItemID=2312398081 That’s all I use. I understand that other people might need more information, and may be handling all kinds of specific situations, but that’s part of the point. Different people need different info, so just dumping all available […]

Rails Migrations, Primary key is not allowed in a has_and_belongs_to_many join table

I was working like a fiend on a project, and forgot to add :id => false to a join table migration. For completeness, here’s the migration as it is supposed to look: class CreateEmployeesRoles < ActiveRecord::Migration def self.up create_table :employees_roles, :id => false do |t| t.references :employee t.references :role end end def self.down drop_table :employees_roles end end As I said, I forgot the :id => false, so it put a primary key on the table, which causes rails to error out with: Primary key is not allowed in a has_and_belongs_to_many join table (employees_roles). K, actually, the id column doesn’t really affect it, but heh it’s not needed, so let’s get rid of it. Unfortunately, you can’t use rake db:rollback, it gives you the same error. Understandable, but technically, that is kind of a bug ala the random feature. If you make a mistake in your migration, ideal you should be able to rollback irregardless of table specific errors (That’s why you’re rolling back in the first place.) In order to fix this, you’ll have to do it via MySQL: $ mysql -u username -p yourapp_development mysql> alter table your_table drop column id; After that you can run: $ rake db:rollback $ rake db:migrate And then you should be nice and fixed up.

Rails Application Config 1

I saw this railscast on application wide configs and I liked it alot, however, I am not big on typing APP_CONFIG[:value][:subvalue] so I have made a small tweak to that, and placed it as a model, which is where I like it to be, as it is a model of the applications configuration, where the datasource is a fixed file, in this case, config/config.yml. I suppose, you could put it anywhere you like. class ValueProxy def initialize(attrs) @attrs = attrs end def method_missing(sym,*args,&block) sym = sym.to_s if @attrs[sym] then if @attrs[sym].class == Hash then return[sym]) else return @attrs[sym] end end end end class AppConfig @@config = YAML::load_file(“#{RAILS_ROOT}/config/config.yml”) def self.method_missing(sym, *args, &block) if @@config[sym.to_s] then if @@config[sym.to_s].class == Hash then return[sym.to_s]) else return @@config[sym.to_s] end end end def self.respond_to?(sym,include_private=false) true end end Here is the yml file: auth: http_basic: true And then the usage: AppConfig.auth.http_basic == true Well that’s more or less it.