Ruby On Rails is Web Development That Doesn't hurt. It abstracts a lot of housekeeping stuff away from you, which is cool
Rails Things I always forget
Selecting an option in a select (aka popup) control
The third parameter for the select_tag must match the first item (aka: the value item) in the options. AKA with an option of [1, "Shipped"], the third item must be one and not the string 1. Use to_i if you're unsure!
Things I hate about Rails/Ruby
* Too easy to get Date, Time, and DateTime confused, with disastrous results
d = DateTime.now d + 3.days puts d # will be something like "2718-12-22T17:04:29-04:00"now you've screwed the pooch and sent d WAAY into the future. Oops. Better to do:d = DateTime.now d.to_time + 3.days puts d #will be something like "Sat Apr 25 17:05:26 -0400 2009"
#to_time works on Times and Dates too -- so this way it won't better what you've been given, just call #to_time on it and add n.UNITS
Language Work-Arounds Provided By Rails
HashWithIndifferentAccess (aka: don't care if you pass a Symbol or a String, it'll return the same key)
Rails, AJAX, jQuery
Rails not decoding JSON from jQuery correct: array becoming a hash with integer keys
Rails Configuration Information
Targetting MacPorts MySQL via sudo gem install mysql:
$ sudo gem install mysql -- --with-mysql-config=/opt/local/bin//mysql_config5
Unpacking Gems Into the vendor/ directory
See Autoloading Gems In Vendor. The comments on this blog entry also points to Dr. Nic's Gems On Rails plugin
Err The Blog explains why this is important
Neat Things Added Since Rails 1.2.3
Named Scopes for models. See The blog article introducing this
Example:
#in the Person model...
named_score :women, :conditions => {:sex => "F"}
# now, in the wild...
all_women_in_system = Person.women
Rails Hacks
Dealing with a ton of migrations
Use schema.rb as migration 1
David Bock says: A common usage of the schema.rb is to handle the collapse of a huge number of migrations. For instance:
I have worked on a rails project that grew to over 120 migrations over the course of two years. Building the db from scratch took a long time, and a lot of that time was spent adding columns that were no longer used, removing columns, transforming data, etc.
We were able to run this stuff at a known release of the software, then take the generated schema.rb file, remove all the migrations up until that point, and make the schema.rb file the new migration #1.
That meant we couldn't roll back past that point without checking out earlier versions of the software, but that was well worth the reduction in complexity.
The RailsKits project do something similar - when you buy a RailsKit, you are buying a partial implementation. You don't care about their migration history, you only need to know the db up until that point. So their first migration is their schema.rb file.
Organizing your Rails codebase (more
Partials: where to put them
Ryan Wilcox: so I've isolated a partial, having to do with showing Products, that we'll use in a lot of places. Should I put that partial in views/products, or views/shared ? Jim Gay: products Jim Gay: that's what it's about Ryan Wilcox: awesome, thanks :) Brian Kidd: not that it matters, but i agree with jim. views/shared is about non-contextual views Brian Kidd: headers, footers, etc... Brian Kidd: as soon as the partial has context of say, a model, then it should go in a structure based on that context
Rails On Windows
Loading a big SQL dump into MYSQL
PHPMySQL likes to time out on big documents. BUT you CAN use mysql directly in InstantRails.
But it's Windows, so you can't use the normal trick of mysql -u root < FILE_TO_IMPORT (no redirect operators)
So if you open a console in InstantRails, you can do
mysql -u root
then use the proper database. THEN
mysql > source FILE_TO_IMPORT
Which will load the file
Various plugins I've used
Websolr
- reindex on local machine
rake sunspot:reindex
- reindex on Heroku
heroku rake sunspot:reindex
- reindex on Heroku if it has a broken pipe:
heroku rake "sunspot:reindex[10]"
heroku rake "sunspot:reindex[10]" --app production_app`
This Comments:
Add comments by visiting: RubyOnRails/Comments