开发者

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

开发者 https://www.devze.com 2023-02-12 12:29 出处:网络
If I have the following models setup: class Member < ActiveRecord::Base has_many :children end class Child < ActiveRecord::Base

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

精彩评论

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