Posts tagged with “Ruby on Rails”
- All (38)
- Entries (17)
- Links (21)
- Photos (0)
The Official Viddler Ruby Gem
I just announced our new Ruby gem over on the Viddler blog. It’s a fairly basic wrapper for our v2 API for now, but I definitely have plans for some really cool features, like having ActiveModel compatible classes for stuff like videos, playlists, users, etc., as well as making it easy to integrate into existing ActiveRecord models in Rails.
So, if you’re into Ruby and you’re looking for a way to integrate video into your site, definitely check us out. We have a really great API, and now that there’s an official gem, it’s easier than ever to get started.
Requiring SSL Using Route Constraints in Rails 3
The new router in Rails 3 makes it super easy to require SSL for certain routes. Just use the following in your config/routes.rb:
MyApp::Application.routes.draw do
class SslConstraint
def self.matches?(request)
request.ssl?
end
end
scope :constraints => SslConstraint do
resources :payments
# Other SSL routes go in here
end
end
Now, this is a pretty simple example–you’ll likely want to also have routes to redirect if a user tries to access without SSL, but it definitely shows off the power of the new router.
Canable: The Flesh Eating Permission System
John Nunemaker releases his super-simple way to implement permissions in a Rails app. I love how this does away with a complicated role system and just deals with whether or not a user can do specific action. Simplicity at its finest.
Eight tips for getting the most out of your Rails app
Some good tips on optimizing your Rails site. I’m already using a couple of these on this site, but I definitely want to try out the rest of them as well.
Filestore Cache Expires_in Plugin
Adds an :expires option to filestore caching in Rails–I’ll definitely have to try this out on this site, as standard cache expiration using sweepers and observers is a huge pain.
That’s Not a Memory Leak, It’s Bloat
Excellent post by the team over at EngineYard on common memory issues in Rails, as well as how to fix them. Also be sure to check out the comments below, as there are a lot more suggestions on how to improve your app.
Compiling Ruby, RubyGems, and Rails on Snow Leopard
Dan Benjamin walks through how to install Ruby, RubyGems, and Rails on Snow Leopard. Also be sure to check out his other Snow Leopard guides for Mercurial, Git, and MySQL.
Building a Video Portal in Rails
Nathaniel Bibler gives an excellent presentation of how EnvyLabs put together 4Kids TV, a video portal for kids’ TV shows. It’s long, but worth it, as he steps through everything it took to get the project done. It’s a great example of why you don’t necessarily need to stick to Rails’ conventions for everything, and in some cases, it’s better to think of Rails as your “glue” between many different parts.
Simple APIs using SerializeWithOptions
Viget Labs rolls out a Rails plugin to cleanup your API’s code.
Instead of doing this in the controller:
@speaker.to_xml(
:methods => [:average_rating, :avatar_url],
:except => [:email, :claim_code],
:include => {
:talks => {
:methods => :average_rating,
:except => :creator_id
}
}
)
You move it to the class like this:
class Speaker < ActiveRecord::Base
serialize_with_options do
methods :average_rating, :avatar_url
except :email,
:claim_code
includes :talks
end
end
Great way to DRY up your code.
ObjectiveResource
Pretty cool:
ObjectiveResource is an Objective-C port of Ruby on Rails’ ActiveResource. It provides a way to serialize objects to and from Rails’ standard RESTful web-services (via XML or JSON) and handles much of the complexity involved with invoking web-services of any language from the iPhone.
I’ve been considering trying out some iPhone development (even though I don’t yet own one), and this seems like a really slick way to interface with an existing Rails application.
Improving on Related Entries
A little while back, I posted about how I was determining related entries for my site. That method worked, but once I redid my site and added my 250+ Flickr photos, it started to really slow down when finding related photos, because of the increase in tags and posts. The real issue was that I was doing most of the work in Ruby, when it really should have been done with SQL. So, I decided to rewrite it.
Note: If you haven’t looked at my previous entry on the subject, you might want to take a look at it, just for the general idea of what I’m trying to accomplish. Essentially, I’m trying to find related posts by comparing tags. Here’s my new Post#related code:
def related(limit=5)
return [] if tags.empty?
join_array = tags.collect {|tag| "posts_tags.id = #{tag.id}"}
tags_join = "AND (#{join_array.join(' OR ')})"
self.class.find(:all,
:joins => "INNER JOIN taggings posts_taggings ON posts_taggings.taggable_id = posts.id" +
"INNER JOIN tags posts_tags ON posts_tags.id = posts_taggings.tag_id #{tags_join}",
:conditions => ["posts.id != ?", id], :group => "posts.id",
:order => "COUNT(*) DESC",
:limit => limit)
end
You can see, as I mentioned, that all the work is being done in the SQL now. I first create a list of tags from the current post, which I then feed into the query to search for other posts with similar tags. The SQL instructs the database to search for any posts with any of these tags, and then orders them based on how many tags match between the 2 posts. The SQL ended up being fairly complicated, with a lot of joins, but it’s now a whole lot faster, because I’m not creating a lot of overhead by dealing with the computation in Ruby. If you’re interested, here’s an example related entry query:
SELECT `posts`.* FROM `posts`
INNER JOIN taggings posts_taggings ON posts_taggings.taggable_id = posts.id
INNER JOIN tags posts_tags ON posts_tags.id = posts_taggings.tag_id
AND (posts_tags.id = 695
OR posts_tags.id = 192
OR posts_tags.id = 195)
WHERE (posts.id != 4322) AND ( (`posts`.`type` = 'FlickrPhoto' ) )
GROUP BY posts.id
ORDER BY COUNT(*) DESC
LIMIT 5
Scaling Rails with Gregg Pollack
Man, how did I miss this before? New Relic is having Gregg Pollack, of Rails Envy fame, do screencasts on scaling Rails. There’s some really great stuff in here, and I definitely need to reserve some time to go through each of them myself.
