当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - 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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: