Getting comparison of Fixnum with nil failed when trying to set restrictions

2.1k views Asked by At

In my app I have a table of venues where each can be on either a free or premium plan and each can have multiple venuephotos.

I'm trying to set it so that free venues can have a maximum of 3 venuephotos whilst premium venues can have up to 10 venuephotos.

Venue.rb

The plans are set as:

PLANS = %w[free premium]

The venuephoto limit is also set as:

def photo_limit
    {:free => 3, :premium => 10}[plan]
end

Venuephoto.rb

I then have this to check if the limit has been reached yet:

validate :venuephoto_count_within_limit, :on => :create

def venuephoto_count_within_limit
  if self.venue.venuephotos(:reload).count >= self.venue.photo_limit
    errors.add(:base, "Exceeded venue photo limit")
  end
end

This is giving me this error:

ArgumentError in VenuesController#update

comparison of Fixnum with nil failed

Venue controller

def update
  @venue = Venue.find(params[:id])
  if @venue.update_attributes(params[:venue])
    flash[:notice] = 'Venue updated successfully'
    redirect_to :back
  end
end

Thanks very much for any help!

1

There are 1 answers

1
Leonid Shevtsov On BEST ANSWER

Your {:free => 3, :premium => 10} hash uses Symbols as indices, and the plan would be a String.

def photo_limit
  {:free => 3, :premium => 10}[plan.to_sym]
end

will work