可以有很多Organisations
User
属于Organisation
App
属于Organisation
Env
属于App
创建Env
并选择App
时
我可以撰写哪种validates
来检查App
是否属于与Organisation
相同的current_user
?
答案 0 :(得分:0)
User.current
是描述为here
validate :app_must_belong_to_the_same_organisation_as_user
def app_must_belong_to_the_same_organisation_as_user
unless User.current.organisation == app.organisation
errors.add(:base, "User's and app's organisations mismatch!")
end
end
答案 1 :(得分:0)
#app/models/app.rb
Class App < ActiveRecord::Base
belongs_to :organisation
validates :app_user_organisation?
private
def app_user_organisation?
organization == [[current_user]] #-> still working out current user
end
end