I’m a struggling newbie to programming and trying to make a freemium style app with multiple plans available.
I’m using the cancan gem to assign venues a role depending on what plan they are on (free, premium, premium+).
I know how I can limit certain options being displayed in the views depending on which plan is assigned but how can I go about limiting number of records added to a venue depending on which plan is assigned?
e.g.
- I would like all venues to have photos but the free venues should be
limited to just 3. - I would also like all venues to have searchable tags but have the number of tags allowed change depending on which plan the venue is on.
Currently the plans are just specified in the venue model as:
PLANS = %w[free premium premium+]
edit
Thanks to the answer from Alex Peattie I think I’m on the right track now.
I changed the line validate_on_create :photo_count_within_limit to:
validate :venuephoto_count_within_limit, :on => :create
but am getting a ArgumentError in VenuesController#update: comparison of Fixnum with nil failed error.
another edit
So all is now well, just made these changes:
def photo_limit
{:free => 3, :premium => 10}[plan.to_sym]
end
and
validate :venuephoto_count_within_limit, :on => :create
Thanks for any help its much appreciated!
I’d give the Venue model methods for
photo_limit,tag_limitetc.(this assumes a
.planmethod which will return the current Venue’s plan)Then use validations in Photo, like so: