1

There can be many Organisations

A User belongs to an Organisation

An App belongs to an Organisation

An Env belongs to an App

When creating an Env an App is chosen

What kind of validates can I write to check that the App belongs to the same Organisation as the current_user does?

Gabriel Baker
  • 1,209
  • 11
  • 21

2 Answers2

0

User.current is a pattern described 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
Community
  • 1
  • 1
Magnuss
  • 2,270
  • 19
  • 21
  • I updated my answer. How you want to access your current_user is up to you. You could as well pass the current_user to the `App` object from controller if you do not want to use `User.current` – Magnuss Apr 18 '14 at 21:10
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
Richard Peck
  • 76,116
  • 9
  • 93
  • 147