I know I am missing something very big regarding the changes with
respect to activerecord in Rails 3.
I can't find the explanation. And I'm sure someone will kick my ass for
finding the right piece of info in the docs and guides. Please do.
I have a working piece of code but I don't like it.
So. Very basic association:
class Project < ActiveRecord::Base
class Task < ActiveRecord::Base
The task table has the foreign key:
I can create a task this way, and the resulting task has the correct
project id stored
@project = Project.find(1)
@task = @project.tasks.build(params[:task])
Now I get the project record using the Rails 2 way:
@project = Project.find(1) triggers:
Project Load (0.2ms) SELECT `projects`.* FROM `projects` WHERE
(`projects`.`id` = 1) LIMIT 1
#<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23",
updated_at: "2010-11-18 15:15:23">
[#<Task id: 1, title: "test", project_id: 1, created_at: "2010-11-18
15:20:06", updated_at: "2010-11-18 15:20:06">]
But now I want to use the Rails 3 way:
@proj = Project.where(:id => params[:id]) trigger:
Project Load (0.3ms) SELECT `projects`.* FROM `projects` WHERE
(`projects`.`id` = 1)
[#<Project id: 1, name: "test", created_at: "2010-11-18 15:15:23",
updated_at: "2010-11-18 15:15:23">]
And @proj.tasks.inspect gives:
NoMethodError (undefined method `tasks' for
Now, if I use:
@proj.tasks.inspect then I get the tasks for the project.
Is this the right way? I have the feeling it is not.