class Project < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :project
end
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@tasks = Task.find(:all, :conditions => ['project_id = ? and complete = ?', @project.id, false])
end
end
This is the stupid way doing this job, let try to find a smarter way to do this, using the association between project and task model.
def show
@tasks = @project.tasks.find(:all, :conditions => ['complete = ?', false])
end
or, even better way:
def show
@tasks = @project.tasks.find_all_by_complete(false)
end
That's to say, project_id can be removed from the query conditions, since it is in the mean of the associations.
阅读(314) | 评论(0) | 转发(0) |