По сути, я хочу, чтобы каждый пользователь («дизайнер») мог добавлять только количество клиентов, ограниченных их планом.Поэтому, если их план разрешает только 1 клиента, это все, что они могут сделать.
Моя модель пользователя выглядит следующим образом:
class User < ActiveRecord::Base
devise :database_authenticatable, :confirmable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable, :invitable
has_many :clients,
:through => :client_ownerships,
:order => 'created_at DESC'
{edited for brevity}
end
Модель клиента выглядит следующим образом:
class Client < ActiveRecord::Base
before_save :number_of_clients
belongs_to :user
has_many :projects, :order => 'created_at DESC', :dependent => :destroy
has_one :ownership, :dependent => :destroy
has_one :designer, :through => :ownership
validates :name, :presence => true,
:length => {:minimum => 1, :maximum => 128}
def number_of_clients
Authorization.current_user.clients.count < Authorization.current_user.plan.num_of_clients
end
end
Модель плана выглядит следующим образом:
# == Schema Information
# Schema version: 20110214082231
#
# Table name: plans
#
# id :integer not null, primary key
# name :string(255)
# storage :float
# num_of_projects :integer
# num_of_clients :integer
# cached_slug :string(255)
# created_at :datetime
# updated_at :datetime
# price :integer
#
class Plan < ActiveRecord::Base
has_many :users
has_friendly_id :name, :use_slug => true
end