Richlewis
Richlewis

Reputation: 15384

Constructing a query in a class method

I am looking to perform a query on a model of mine (postgresql) as a validation.

I want to count the number of times a paticular skill_id occurs in the model, and if it is >= 3 for example then validation fails

so far I have

class Document < ActiveRecord::Base
 mount_uploader :media, MediaUploader
 belongs_to :user
 belongs_to :skill

 validate :num_of_images

 def num_of_images
   skill_id = self.skill_id
   number = self.count(skill_id: skill_id)
 end
end

but i get

(undefined method `count' for 1:Fixnum):

What method/query do i need to use to count the records

Thanks

Upvotes: 0

Views: 25

Answers (1)

Manu Gambino
Manu Gambino

Reputation: 64

Please update question with model rb files and an example of when it fails the validation, I dont understand what you are trying to do yet

Update

Replace self with Document

Document.where(skill_id: skill_id).count

In that context self is not the clase but a Document object

Upvotes: 1

Related Questions