Gidpoiiohika
Gidpoiiohika

Reputation: 23

How to implement the method that should return the skill

I can not understand how to implement the primary_skill method. This method should return a skill that is marked as primary user.primary_skill

class User < ApplicationRecord
  has_many :user_skills
  has_many :skills, through: :user_skills
end

class Skill < ApplicationRecord
  validates :title, presence: true, uniqueness: true

  has_many :user_skills
  has_many :users, through: :user_skills
end

class UserSkill < ApplicationRecord
  belongs_to :skill
  belongs_to :user
end

create_table "user_skills", force: :cascade do |t|
  t.boolean "primary", default: false
  t.bigint "skill_id"
  t.bigint "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["skill_id"], name: "index_user_skills_on_skill_id"
  t.index ["user_id"], name: "index_user_skills_on_user_id"
end

Upvotes: 2

Views: 87

Answers (1)

kiddorails
kiddorails

Reputation: 13014

Answer by @Alec will almost certainly result in syntax error, the block cannot logically evaluate that expression.

You can achieve that by following:

class User < ApplicationRecord
  has_many :user_skills
  has_many :skills, through: :user_skills
  has_many :primary_user_skills, -> { where(primary: true) }, class_name: 'UserSkill'
  # Above cannot be has_one, we are creating intermediate relationship from user_skills where primary is true.

  def primary_skill
    primary_user_skills.first.try(:skill)
  end
end

Edit - Simplified version for same can be below:

class User < ApplicationRecord
  has_many :user_skills
  has_many :skills, through: :user_skills

  def primary_skill
    skills.find_by(user_skills: {primary: true})
  end
end

Upvotes: 3

Related Questions