Plugin: attachment_fu

Posted by topher
on Thursday, July 19
attachment_fu is a rails plugin written by Rick Olson. From the README,

attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.

Check out Mike Clark’s tutorial.

On windows, when I submit the form with the file upload, I get an error “Size not included in list”. Upon inspection, the size is zero. (The default minimum size is 1 byte.) The size of the file uploaded isn’t set properly. I found somewhere on the beast forum that you need to add “sleep 1” on your controller. I haven’t tried to use attachment_fu on my server (ubuntu) and I’m hoping this is only a problem with windows.

My friend Mon, also a rails developer, encountered another problem with attachment_fu. He added a user_id in the model which uses attachment_fu. When he saves the uploaded file, he gets an error saying that user_id is null. We’re absolutely sure there’s a user_id.

Here’s what happened: attachment_fu can create a thumbnail(s) for you when you save the uploaded image (you will need an image processor like image magick). If you don’t specify a thumbnail_class, attachment_fu will use the same model for the thumbnail. So if you have a Mugshot model, after submitting the form, 1 model will be created for the uploaded image, and another one will be created for the thumbnail. So even if you add user_id to your model, the thumbnail won’t have a user_id. You need to add a callback before_thumbnail_saved on your model.

1
2
3
before_thumbnail_saved do |record, thumbnail|
  thumbnail.user_id = record.user_id
end

Install the plugin with

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/

Editing models in migrations

Posted by topher
on Friday, July 13

In migrations, you can also edit your models. I have a Topic and a Post. A topic has many posts. The models are already created using previous migrations. Now I want to add 3 new fields to Topic—posts_count, replied_by, and replied_at.

Here is the migration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class AddCachedFieldsOnTopic < ActiveRecord::Migration
  class Topic < ActiveRecord::Base
    has_many :posts, :order => "posts.created_at"
  end
  class Post < ActiveRecord::Base; end
  
  def self.up
    add_column :topics, :posts_count, :integer, :default => 0
    add_column :topics, :replied_by, :integer
    add_column :topics, :replied_at, :datetime
    
    Topic.find(:all, :include => :posts).each do |topic|
      last_post = topic.posts.last
      posts_count = topic.posts.size
      Topic.update_all(["posts_count = ?, replied_by = ?, replied_at = ?", posts_count, last_post.user_id, last_post.created_at], ["id = ?", topic.id]) if last_post
    end
  end

  def self.down
    remove_column :topics, :posts_count
    remove_column :topics, :replied_by
    remove_column :topics, :replied_at
  end
end
Notes
  • posts_count will be used by rails for caching. In the post model, I’ll put :belongs_to :topic, :counter_cache => true. Set the default to 0.
  • You can actually get these fields through the association—topic.posts.count, topic.posts.last.user, topic.posts.last.created_at – but I’m adding it to Topic to avoid the additional queries.
  • I’m looking at (copying) the beast application.

Server Setup

Posted by topher
on Thursday, July 05

I have a VPS from Rimuhosting and I’m using nginx and a cluster of mongrel for my rails applications. I followed this tutorial which guides you on setting up everything – ruby, rails, nginx, mongrel, postgresql, subversion and capistrano. I’m sharing my vps with some friends and ruby, rails, and mysql were already installed. I’m using Dreamhost for subversion. Setting up nginx and mongrel is easy. I just followed the tutorial and didn’t encounter any problem.

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, Camping, and IOWA frameworks.

I use nginx for this reason (taken from the faq page in mongrel website):

Ruby on Rails is not thread safe so there is a synchronized block around the calls to Dispatcher.dispatch. This means that everything is threaded right before and right after Rails runs. While Rails is running there is only one controller in operation at a time. This is why people typically have to run a small set of Mongrel processes (a “Pack of Mongrels”) to get good concurrency.

For a rails application, I run 2 mongrel servers. I use the gem mongrel_cluster to manage these mongrels (this is in the tutorial). For example, mongrels are running in port 7500 and 7501. When a request is made, nginx passes that request to one of the mongrels. For bigger applications, use more mongrels.

Since I’m sharing the VPS with my friends, I can’t change the web server easily. Besides, they are running php and though it’s possible to run php in nginx I haven’t researched that yet. We are using Apache 2.0 which is not easy to set up with mongrel. Apache 2.2 is recommended. I have to run nginx on a different port, say 8088. Then on the apache conf file, I put this

1
2
3
4
5
6
7
8
9
10
<VirtualHost *:80>
        ServerName topher.88-mph.net
        ServerAdmin crigor@gmail.com
        ProxyRequests Off
        ProxyPreserveHost on
        <Location />
          ProxyPass http://topher.88-mph.net:8088/
          ProxyPassReverse http://topher.88-mph.net:8088/
        </Location>
</VirtualHost>

When you go to topher.88-mph.net, apache gets the request then passes it to ngingx which in turns passes it to one of the mongrels. Apache is not really needed on this set up.