如何确保使用正确的帐户在rails中保存模型?

时间:2011-02-22 20:28:18

标签: validation ruby-on-rails-3

如果我有以下型号设置:

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辅助方法,模型中似乎没有这个方法

1 个答案:

答案 0 :(得分:0)

所以,从我收集的内容来看,“Rails Way™”就是将条件放在控制器中。

例如:

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