这是我的模型:
class Hour < ActiveRecord::Base
attr_accessible :time, :user
belongs_to :project
end
class Project < ActiveRecord::Base
attr_accessible :name
has_many :hour, :dependent => :destroy
end
我正在尝试做这样的事情:
hour = Hour.new
#add values to the hour object here
hour.save!
project = Project.find :first
project.hour.add hour #how do I actually do this?
projet.save!
这会引发错误。如何将模型添加到关联?
我来自 Doctrine2 的 PHP 背景。在 Doctrine2 中,我会执行以下操作:
$projects->getHours()->add($hour);
另外,我已经阅读了这些文档: http: //guides.rubyonrails.org/association_basics.html。它们似乎涵盖了有关如何创建关联的所有内容,但我找不到有关如何使用它们的信息!关于如何使用关联的任何好的文档?