我有任务和用户。当用户完成一项任务时,我会创建一个 Completion,其中有一个字段供用户指示他们花费了多长时间。我需要一个表单来显示所有任务及其完成状态和 time_spent 属性。提交时,应该更新存在的完成并创建新的完成。如果可能的话,我想在 Formtastic 中执行此操作,但我会对基本的 Rails 3 解决方案感到满意。
class Completion < ActiveRecord::Base
belongs_to :task
belongs_to :user
# attribute time_spent
end
class User < ActiveRecord::Base
has_many :completions
has_many :tasks, :through => :completions
end
class Task < ActiveRecord::Base
belongs_to :milestone
has_many :completions
has_many :users, :through => :completions
end
一个额外的方面是我只想显示一组特定的任务,例如属于里程碑的任务。我是否应该在 Milestone 控制器上创建一个表单来发布到 Completions 控制器?
class Milestone < ActiveRecord::Base
has_many :tasks
has_many :completions, :through => :tasks
end
更新我已经找了好几天了, 我发现了很多 死胡同 。Rails 表单中的Multiple objects很接近,但它要求所有链接对象都已经存在。
这个问题的不同之处在于,一些链接还不存在,并且没有用于嵌套链接的单一模型。例如,Ryan Daigle 的嵌套对象表单帖子)我已经在一个表单中进行了这项工作以进行编辑用户的所有可能完成,但我需要以一种形式编辑可能完成的子集。我是否需要为has_many
Completions 和belongs_to
User 创建一个冗余对象 MilestoneCompletions?ActiveModel 可以has_many
吗?