diff options
Diffstat (limited to 'app/models/ml/model.rb')
-rw-r--r-- | app/models/ml/model.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/app/models/ml/model.rb b/app/models/ml/model.rb index 684b8e1983b..fb15b9fea72 100644 --- a/app/models/ml/model.rb +++ b/app/models/ml/model.rb @@ -2,6 +2,8 @@ module Ml class Model < ApplicationRecord + include Presentable + validates :project, :default_experiment, presence: true validates :name, format: Gitlab::Regex.ml_model_name_regex, @@ -14,6 +16,10 @@ module Ml 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 @@ -21,5 +27,10 @@ module Ml 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 end end |