Hi all, I''m pretty new to Rails, and having a problem with :finder_sql. I''ve got something like this : class Project < ActiveRecord::Base has_many :jobs has_many :recently_updated_tasks, :class => "Task", :finder_sql => "SELECT tasks.* FROM tasks INNER JOIN jobs ON tasks.job_id = jobs.id WHERE job.project_id = #{id} ORDER BY tasks.updated_at DESC LIMIT 10 end class Job < ActiveRecord::Base belongs_to :project has_many :tasks end class Task < ActiveRecord::Base belongs_to :job end The idea is that each Project can list the 10 most recently updated tasks within all jobs within that project. In the Project controller, I then do something like : def show: @project = Project.find( @params[ "id" ] ) @recently_updated_tasks = @project.recently_updated_tasks end Calling "/project/show/1", Everything seems to be OK, but no recently updated tasks are listed. Looking in development.log, I get something like : Task Load (0.018164) SELECT tasks.* FROM tasks INNER JOIN jobs ON tasks.job_id = jobs.id WHERE jobs.project_id = 11120688 ORDER BY updated_at DESC 10 Note the ID being a large number. What am I doing wrong? Am I misunderstanding the use of #{id} in :finder_sql in the Project model? Or am I going about acheiving this in completely the wrong way? Any pointers would be gratefuly received. Regards, Carl.
carl wrote:> Hi all, > > I''m pretty new to Rails, and having a problem with :finder_sql. I''ve > got something like this : > > class Project < ActiveRecord::Base > has_many :jobs > has_many :recently_updated_tasks, :class => "Task", :finder_sql => > "SELECT tasks.* FROM tasks INNER JOIN jobs ON tasks.job_id = jobs.id > WHERE job.project_id = #{id} ORDER BY tasks.updated_at DESC LIMIT 10 > end >Carl, Single quote your finder_sql string, with double quotes #{id} is being interpolated early, instead of when the query fires off. That''s why you''re seeing a bogus id in your logs. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
carl wrote:> I''m pretty new to Rails, and having a problem with :finder_sql. I''ve > got something like this : > > class Project < ActiveRecord::Base > has_many :jobs > has_many :recently_updated_tasks, :class => "Task", :finder_sql => > "SELECT tasks.* FROM tasks INNER JOIN jobs ON tasks.job_id = jobs.id > WHERE job.project_id = #{id} ORDER BY tasks.updated_at DESC LIMIT 10 > end > > What am I doing wrong? Am I misunderstanding the use of #{id} in > :finder_sql in the Project model? Or am I going about acheiving this in > completely the wrong way?When you use "double quotes" the string is interpolated immediately in Project class. When you use ''single quotes'' the string is interpolated by Rails in the context of a Project instance. Use single quotes & you''re good to go. jeremy
> Single quote your finder_sql string, with double quotes #{id} is being > interpolated early, instead of when the query fires off. That''s why > you''re seeing a bogus id in your logs.Excellent! I''ve ran into this problem I-don''t-know-how-many-times in various languages, didn''t even think about it in this case. Thanks a lot guys, much appreciated. Regards, Carl.