Is the Rails' find (x) method on the model lazy? If not, what is equivalent?
I am new to Rails, so I discovered that I am writing such areas:
class Course < ActiveRecord::Base
scope :by_instructor_id, lambda { |instructor_id| where(:instructor_id => instructor_id) }
scope :by_course_template_id, lambda { |course_template_id| where(:course_template_id => course_template_id ) }
scope :by_company_id, lambda { |company_id| joins(:instructor).merge(CompanyUser.by_company_id(company_id)) }
end
This is not a lot of work, but now I ask myself ... if Rails provided them with scope, I would not have to write them.
So, does Rails offer? Can I do something like the code below and make it just one database call?
Company.find(params[:id]).instructors.courses
instead
Course.by_company_id(params[:id])
What is right? I know that Course.by_company_id(params[:id])this is only 1 database call. This is very well known for writing SQL or queries in Hibernate. But if you can write it in another way, maybe you need to?
Company.find(params[:id]).instructors.courses, . , , 3 , , , Company.find(x) . ?