Archive for the ‘Projects’ Category
Setting up Ruby on Rails in OS X
I consulted quite a few sources online to try and figure out how to do this, and ran into a few problems that took entirely too much time to solve. Now that I’ve figured them out, I’m writing the solutions here in the event I need them again. Hopefully they’ll be useful to a few other people as well.
Goals
Create a Ruby on Rails development environment with Apache, MySQL, and PHP.
There are several possible ways of doing this, but since OS X is a Unix-based platform, I don’t see any reason to use pre-compiled packages like MacPorts and replacing the built-in software. Plus, this is supposed to be a learning experience.
Sources
http://maestric.com/doc/mac/apache_php_mysql_snow_leopard
http://michaelgracie.com/2009/09/23/plugging-mcrypt-into-php-on-mac-os-x-snow-leopard-10.6.1/
Counting records by category [RoR]
In my Rails application, I have an inventory of items described by three models: the items (cryovials), the place they’re stored (dewar), and the information about which vials is stored where (ln2_locations).
The reason I used three models, rather than rolling the attributes of the third (ln2_locations) into the actual item was that in reality, we normally freeze down multiple vials with the same content at the same time. I would like to implement this kind of mass assignment at a later time.
Anyway, it is often useful to be able to determine what we have stored, and how much of each different type of content is present. It’s one thing to know what the SQL code would be to retrieve these records; it’s another to convert that to an ActiveRecord query when you’re just learning.
What I would like to do is look at my items (cryovials), and pull out the unique content values (e.g. cell types), then go back and count how many vials I have with that content. It would be pretty easy to accomplish this as two separate statements, but that would involve iteration and two separate SQL queries. Best to leave it as one query where I count and select unique fields simultaneously:
Rails:
@summary = Ln2Location.select("count(*) count, contents").joins(:cryovial).where(:present => true).group(:contents)
SQL:
SELECT count(*) count, contents FROM `ln2_locations` INNER JOIN `cryovials` ON `cryovials`.`id` = `ln2_locations`.`cryovial_id` WHERE (`ln2_locations`.`present` = 1) GROUP BY contents
I’m selecting on Ln2Locations because there’s a property, “present,” which I’m using to select only the vials which haven’t been physically removed from the collection. Additionally, the relationship between the models is as follows:
class Cryovial < ActiveRecord::Base # Relationships belongs_to :user has_many :dewars, :through => :ln2_location has_many :ln2_locations accepts_nested_attributes_for :ln2_locations
class Dewar < ActiveRecord::Base # Relationships belongs_to :laboratory belongs_to :location has_many :cryovials, :through < :ln2_locations
class Ln2Location < ActiveRecord::Base # Relationships belongs_to :dewar belongs_to :cryovial
So only the child model, Ln2Locations, contains the foreign key that would be used to generate the join table.
Ruby on Rails: Integrating Devise
It is possible to integrate devise into your rails app as part of your user model in order to provide authentication support.
- add desired attributes to migration file
- make added attributes accessible in model file
- either roll your own views or add fields to generated views
That’s it. It really isn’t difficult or different than making any other model.
I was worried that it would be complicated, and spent a lot of time reading about devise to be sure. It’s so easy, no one writes about it.
Database Optimization: Indexing
After installing devise, I was looking through the migration file, and noticed a couple add_index commands. Reading up on what this actually does, I came across a couple interesting articles about database optimization:
Optimizing your MySQL Application
details: http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M001468&name=add_index
I don’t plan to build a giant Rails application, but Rails is known to have scalability issues. Still good to be aware of these points.
Ruby on Rails: on shared hosting, user authentication
* As usual, this post on Ruby on Rails is more documentation of my development process to supplement my notebook and memory. I can try to answer questions, but please keep in mind that I’m learning too.
I’ve bought the eBook version of the 4th edition of the Pragmatic Programmers’ Agile Web Development with Rails. It’s still in beta, but my main goal is to see how things have changed between Rails 2.x and Rails 3. The book also covers some basic user authentication, but it’s very simplistic. In the interest of getting my project up and going quickly, I looked into authlogic and devise/warden.
Both authlogic and devise/warden were highly recommended by mischa after building an application that did some direct comparisons of several different RoR user authentication solutions. Good enough for me. devise appears to be more lightweight, so I’ll try that one first.
A little bit about my server setup: I’m hosted with Lithium Hosting, which is a great deal and has fantastic customer service. They’re not paying me to pitch the service, though clicking the link above and then signing up for a plan gives me referral credits or something. It’s a shared hosting service, so I manage my site with cPanel if I need/want to, and I’ve added on SSH access to my account so I don’t really need to use cPanel.
Once the server upgraded to Rails 3.0/Ruby 1.8.7, a couple things happened: my rails apps stopped starting automatically (cPanel feature), cPanel could no longer start my rails apps, and I couldn’t create a new Rails app with a MySQL database.
Solutions:
1) install the mysql2 gem, which is the new mysql database adapter used by Rails 3. The compilers are disabled on my host, so this was done via support ticket.
2) add mongrel to my Gemfile,
#project/Gemfile
gem 'mongrel'
Mongrel isn’t automatically started in development mode. cPanel still won’t start my application, so starting the server is done via SSH with
rails server --port=[port]
So now I can create a new project
rails new project --database=mysql
and start the server. However, I don’t have a good way of authenticating users.
So, back to the support tickets to have both authlogic and devise installed. Once tech support has done that (seven minutes. really.), check if they’re installed for me:
gem server --port=[port]
and navigate to mysite.com:[port]. They’re not, run a couple commands from the command line:
gem install devise
gem install authlogic
Which should install the gems and make them available to me. Checking again as above, and both devise and authlogic are present. Add the appropriate lines to my Gemfile. Then, since I’m trying out devise first, install it:
rails generate devise:install
That’s all for now.
Word Clock Project, Additional Thoughts
Examining the posted schematic, PCB layout, and parts list for the Word Clock at Instructables, there appear to be several inconsistencies. I believe that this is due to the fact that the project was revised a few times after the schematic was drawn. Improvements
- It should be possible to swap out the PIC16F877 that Doug used for a PIC16F887, which has the same pinouts, with minimal changes to the code, should you decide to rebuild his clock without any modifications aside from upgrading the microcontroller. And added benefit of using the 887 versus the 877 is that the 887 has a built-in oscillator with a default frequency of 4MHz, so an external oscillator is no longer needed.
Inconsistencies
- The parts list supplied by Doug lists the power regulator as a 78L05, whereas the outline on the PCB layout is for an LM7805.
I had a few other things in mind, but I originally drafted this post a while ago, and now no longer remember what else I was going to write.
Word Clock Project, Initial Impressions
A while ago, Sumeet and Ashley came across a clock which displayed time using words rather than numbers. It’s nice, but expensive at €885 or €1,099 for the new stainless steel version.

Nice, but not €889 nice.
Someone named Doug from Australia (?) came up with an Instructable detailing how to build your own Word Clock for significantly less money, though it may take some refining to make it pretty.

I don't know Doug or Megan.
This guy Doug also redesigned the faceplate for the clock to include both his name and I’m assuming the name of his significant other, Megan. Since I already plan to change the circuit, I’ll probably change this too, since I don’t have any good friends named Doug or Megan, and certainly not a Doug or Megan who I’m both friends with and are significant to one another.
Proposed Modifications:
It seems that the new version have four extra LEDs, one at each corner, to provide more accurate time. The original clock could be accurate to within five minutes, displaying something like, “It is five past one”. The new clock should keep track of exactly which minute in that five minute interval it actually is, e.g. 0 = x:x0 or x:x5, 1 = x:x1 or x:x6, 2 = x:x2 or x:x7, etc. This should be relatively easy to implement — the easiest would be to use one of the pinouts from the PIC microcontroller to send a digital signal to either a series of transistors that would essentially count to five and reset. Since I don’t have a whole lot of experience designing that kind of a circuit, the fastest way for me would probably to use a shift register to virtually expand the number of pinouts and address each LED for the minute counting individually.

You can't turn it off.
David suggested that an extra button be added in order to disable illumination of the clock by the LEDs, in case you know, you wanted to actually go to sleep in the same room as that clock. This would maybe need a pusbutton connected to a comparator that would be connected to an input pin on the micrcontroller, then using one of the pinouts from the microcontroller connected to a transistor to turn on and off the power connection for the LEDs.
In addition, the original clock has 110 characters in an 18″x18″ square frame with a fairly large border around the text area. I’m guessing each letter in the text area has a square inch to itself, for a text area dimension of 11″x10″ (WxH). A problem I see with the clock made according to the Instructable is that there are a few more letters, 117 total, in what seems to be either the same or a smaller area (likely smaller), which results in some light leakage between and around characters being backlit. Therefore, sufficient spacing between characters for dividers to segregate the various words from one another will be necessary. One possible root of the problem for the Instructables clock is spare PCB panels were used as baffles, and if these spare pieces were etched to remove the copper first, then they would be translucent, leading to light leakage.
Cost Issues
The single largest component cost for this project would probably be the circuit board — turns out they’re really expensive, either to have printed or even if you’re going to use a generic board and do all the wiring yourself. Looking just at the circuit board needed to mount the LEDs for the display, the costs are pretty high, and this doesn’t even include the etched circuit board used as a face for the clock, or the controller board. For example, this 10.5″x10.5″ circuit board would be perfect as a surface to mount the LEDs and necessary transistors and resistors, but it’s $62, or $1.78/sq. in.
It is also possible to etch your own board, and at least for the display portion of the clock, this would require something 10″ square or larger. Digi-Key has 12″ square unpunched, copper-clad boards starting at $19 single-sided, 1 oz. copper, or $21.50 for double-sided. The price goes up significantly for pre-punched boards that are copper-clad.
Thinking about this more, it should be possible to shrink the face of the clock, as long as the size of the letters were shrunk as well, which would also reduce the cost quote a bit.
In any case, the third circuit board option would be to design it with some CAD software and have it professionally printed, like from any of these places: barebonespcb.com, expresspcb.com, or batchpcb.com to name a few.

Why use screw terminals?
It seems the best option would be to divide the project between hand-wired parts on a board and a professionally-printed board. The controller board should be professionally printed, and the LED backing for the clock should be wired by hand, with ribbon cable used to connect the two halves via the screw terminals, or better yet, a pair of sockets. The face of the clock could be etched as before, though I’m sure there’s a better way.