Rails, Paginator et has_many associations

Par défaut, le mélange de ces 3 ingrédients ne fonctionne pas sous Rails.

La solution est là :

I dove into RoR Pagination yesterday (ActionController::Pagination), and while it’s great for paginating all data in a model, it really sucks for paginating data from an activerecord association (and by really sucks I mean doesn’t support).

Here’s the problem. Say I have a user who has a collection of photos:


class User < ActiveRecord::Base
has_many :photos, :conditions => 'photos.is_deleted = false', :order => 'photos.created_at DESC'

end

I want to have the ability to feed the user’s photos to the paginator. Unfortunately, the paginate function simply takes a collection id, and will not accept a set of data (an association, in this case). So, if I want to paginate photos on a user, I have to do this:


@photo_pages, @photos = paginate :photos,
:conditions => ["photos.user_id=? AND is_deleted = false", @user.id],
:order => "photos.created_at DESC"

What a mess. Now i’ve lost the benefits of my association, since I have to define the association as part of the pagination rules. Very suprised Rails handles things this way, as it seems to violate the basic DRY principles. Anyways, I only had to write code like this a few times to realize how much of a pain in the ass it is, and I created a “paginate_association” method to help me out.


def paginate_association(object, assoc_name, find_options = {}, per_page = 10)
page = (params[:page] || 1).to_i

item_count = object.send(assoc_name.to_s + '_count')
offset = (page-1)*per_page

@items = object.send(assoc_name).find(:all, {:offset => offset, :limit => per_page}.merge(find_options))
@item_pages = Paginator.new self, item_count, per_page, page

return @item_pages, @items
end

I added this to my ApplicationController (application.rb), and now I can paginate assocations til the cows come home.


@photo_pages, @photos = paginate_association @user, :photos, :order => 'created_at'

This helper uses the Rails Pagination stuff, so you can easily use paginate or paginate_association with the same view. Great!

You can also pass additional “find” arguments, such as :order, :include, :join, etc…

Hopefully this is as useful for you as it’s been for me!

source

Pourquoi les migrations RoR sont un bon système ?

La réponse est ici : http://www-128.ibm.com/developerworks/java/library/j-cb08156.html

Extrait :

Currently, persistence frameworks use one of two approaches: mapping or wrapping. Mapping solutions let you create independent database schemas and object models, and then use a layer of software to manage differences between the two. Mapping solutions seek to build an object model that closely resembles the structure of the database schema. In contrast, wrapping solutions use objects as wrappers around database tables and rows to manipulate data in a database. Conventional wisdom is that a mapping solution is often more flexible once the solution has been released into the wild because mapping software can better deal with changes in the schema or object model. But that wisdom ignores the most important part of the equation: data. To manage any application change involving a persistent domain model effectively, you must coordinate changes in the data, schema, and model. Most project teams don’t get it right. 

Retrouver ses onglets ouverts sous Firefox

Une option toute simple mais qui d’un point de vue ergonomique est à une place discutable dans les préférences de Firefox.

Je cherchais depuis quelques temps l’option qui me permettrait de retrouver tous mes onglets ouverts lors de la dernière utilisation de Firefox. Cette option n’est pas dans la partie “Onglets” ou “Tabs”. Cette option est la première de toutes les options au niveau de “Démarrage” ou “Startup”, dans la liste déroulante de choix.

Qui veut un mignon dragon qui vous suit du regard ?

Une illusion d’optique FANTASTIQUE (à imprimer soi même) :

La Dragon Illusion est ici : http://mightyillusions.blogspot.com/2006/03/dragon-illusion.html

Il faut le faire pour le voir pour le croire !

Si vous ne connaissiez pas cet excellent blog, je vous le conseille :http://mightyillusions.blogspot.com/

Edit : également à monter soi-même, la skeleton illusion : http://ravensblight.com/Illusion.html

ShipIt reprend du service !

Manifestement, le service ShipIt qui permet de commander gratuitement des CDs pressés d’Ubuntu LTS – 6.06 -, a relancé le processus d’envoi des commandes.

En effet, depuis plusieurs mois, toutes les demandes étaient systématiquement refusées.

Voici mon historique :

    • 3 CDs requested in 2006-06-12. 3 CDs approved and sent to the shipping company in 2006-06-13. Please note requests usually take from 4 to 6 weeks to deliver, depending on the country of shipping.
    • 10 CDs requested in 2006-07-31. This request was not approved, so no CDs were shipped.
    • 5 CDs requested in 2006-10-27. This request was not approved, so no CDs were shipped.
    • 10 CDs requested in 2006-12-29. 10 CDs approved and sent to the shipping company in 2007-01-09. Please note requests usually take from 4 to 6 weeks to deliver, depending on the country of shipping.

Merci !