开发者

How to ensure a model is saved with the right account in rails?

If I have the following models setup:

class Member < ActiveRecord::Base
  has_many :children
end

class Child < ActiveRecord::Base
  belongs_to :member
  has_many :phot开发者_JAVA百科os
end

class Photo < ActiveRecord::Base
  belongs_to :child
end

When a new Photo is created, what is the best way to ensure that it is associated with a child in the member's account?

I have login working properly, and a current_member helper method, which doesn't seem to be available in the models


So, from what I gather, the "Rails Way™" of doing this would be to put the conditions in the controllers.

ex:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜