Archive for the ‘ruby on rails’ tag
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.
Ruby on Rails: Migrating data, too
Sometimes it makes sense to re-organize information in such a way that changing the database structure is necessary. It’s not terribly difficult to do this in Ruby on Rails.
I’m writing this more to file this away somewhere I won’t accidentally delete it:
Read the rest of this entry »
Ruby on Rails IDE
I’ve been trying to get back into web development as a hobby; there are many reasons for this, but maybe the most relevant at the moment is that it has the potential to make my actual job easier. If I can develop a website as an online tool, maybe best described as a content management system on steroids and tailored to my needs in the lab, I can spend more time planning and performing experiments, and less time actually running the lab by doing it more efficiently.
Recently, I’ve been using puTTY, WinSCP, and a text editor to build this project in Ruby on Rails. The problem is, I need a lot of real estate on my screen to be able to do this effectively, and I wasn’t able to get WinSCP to open all of my script files in the same text editor window. Combine this with the fact that I had to “touch” every file from the command prompt using puTTY after making changes in the text editor, and saving the modified file to the site with WinSCP — it was really just the least efficient process for designing and building a system to make my life more efficient.
Anyway, a couple years ago, I had stumbled upon RadRails, which was then rolled into the Aptana IDE. Looked good. It had syntax highlighting, which was a big plus. However, SFTP support for remotely working on a RoR project was, at the time, only supported in the paid, “Pro” version of the Aptana Studio suite.
Fast forward to this afternoon, when I re-discovered the Aptana Studio and RadRails, and then found out that the “Pro” version had been scrapped, and now SFTP is available for free!
I’m playing around with it now, but I’m happy so far.