1class Note < ActiveRecord:: Base
 
2    
 
3    belongs_to :user
 
4    
 
5    validates :content, presence: true
 
6    validates :user_id, presence: true
 
7    validates :project_id, presence: true
 
8    validates :category, presence: true
 
 9    
 
10    validate :note_has_atleast_one_character
 
11    validate :note_category_should_among_valid_values
 
12    validate :user_exist, on: :create
 
13    validate :project_exist, on: :create
 
 
 
16    private 
 
17        def note_has_atleast_one_character
 
18            tmp = content.strip
 
19            if tmp.length < 1
 
20                errors[:content] = "note content should has at least one character"
 
21            end
 
22        end
 
 
24        def note_category_should_among_valid_values
 
 
26            if category != 1 and category != 2 and category !=3
 
27                errors[:category] = "note category is not among valid values"
 
28            end
 
 
30        end
 
 
32        def user_exist
 
 
34            user = User.find_by(id: user_id)
 
35            if user == nil
 
36                errors[:user] = "user belong to Note is not exist"
 
37            end
 
 
39        end
 
 
41        def project_exist
 
 
43            project = Project.find_by(id: project_id)
 
44            if project == nil
 
45                errors[:project] = "project should be exist"
 
46            end
 
 
48        end
 
 
50end