如何确保在rails中使用正确的帐户保存模型?
问题描述:
如果我有以下型号设置:如何确保在rails中使用正确的帐户保存模型?
class Member < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :member
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :child
end
当创建一个新的照片,什么是确保其在会员账户孩子相关的最佳方式?
我已经登录正常工作,并且current_member helper方法,它似乎并没有在模型中可
答
所以,从我收集的“Rails的成功之道™”这样做的将是把条件放在控制器中。
例如:
unless current_member.children.collect { |child| child.id.to_s }.include?(@photo.child_id)
@photo.errors.add :child_id "this is not your child"
end