Has_and_belongs_to_many count validation

I am using the has_and_belongs_to_many association and I would like to
limit the number of records that can be joined. I can figure out how to
do it. I can validate on update using this in the model:

def validate
if self.bartypes.count > 4
errors.add_to_base(“Please only select 4 Bar types”)
end
if self.restauranttypes.count > 4
errors.add_to_base(“Please only select 4 Restaurant types”)
end
end

That works great after the record is created but it doesnt work on
create because I would guess that there is no way to count the records
before they are created. Is there a way to count the params in the
model? I could just count the number of records that the form sends.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs