Wednesday, November 09, 2005

Find_by_sql

Although using @company.projects is useful for getting all of @company's projects, I've been having a problem getting at the projects owner. The reason is that rails returns a hash combining the projects and projects_companies tables. As both of these have a column named company_id, when I iterate through @company.projects and try to get at the company_id and hencethe project owner, I get the company_id from the join table and not the company_id from the projects table. I've toyed with the idea of removing the duplication by replacing company_id with owner in the Projects table but want to keep things as they are.

Find_by_sql to the rescue. I simply built the query I wanted using MySQL Query Browser and pasted that in et voila! - I get only the values from the Projects table.