1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# frozen_string_literal: true
module Ml
class Model < ApplicationRecord
include Presentable
validates :project, :default_experiment, presence: true
validates :name,
format: Gitlab::Regex.ml_model_name_regex,
uniqueness: { scope: :project },
presence: true,
length: { maximum: 255 }
validate :valid_default_experiment?
has_one :default_experiment, class_name: 'Ml::Experiment'
belongs_to :project
has_many :versions, class_name: 'Ml::ModelVersion'
has_one :latest_version, -> { latest_by_model }, class_name: 'Ml::ModelVersion', inverse_of: :model
scope :including_latest_version, -> { includes(:latest_version) }
scope :by_project, ->(project) { where(project_id: project.id) }
def valid_default_experiment?
return unless default_experiment
errors.add(:default_experiment) unless default_experiment.name == name
errors.add(:default_experiment) unless default_experiment.project_id == project_id
end
def self.find_or_create(project, name, experiment)
create_with(default_experiment: experiment)
.find_or_create_by(project: project, name: name)
end
def self.by_project_id_and_id(project_id, id)
find_by(project_id: project_id, id: id)
end
end
end
|