Having class methods similar to instance methods for associations

Consider the following example.

class Project
has_many :tasks

class Task
belongs_to :project

@project = Project.find(1)


In all these cases @project is needed. actually we need only
project_id for all the above things.

If the below given things are possible the would save us from the
query @project = Project.find(1)


If this thing can be added to rails it would be great.


What is the real gain here, except avoiding loading the project record in memory? If that is the reason, we can already do that:

Task.new(params[:task].merge(:project_id => project_id))

I agree this syntax is more verbose, but perhaps it should stay this way so the developer doesn’t have to make too many decisions on which API to use.

The frequent mistake for underexperienced Rails devs using AR associations is that they tend to call “Project.tasks”, forgetting that “tasks” is an association defined on a particular Project instance, not at class level. Adding all associations to class level will just confuse people, IMO.