0

I need to display the Specialization that are not selected. User has_and_belongs_to_many specializations and Specialization has_and_belongs_to_many user. Is there any possible to display the specializations that are not selected by the current user.

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :doorkeeper

  belongs_to :user_type, optional: true

  has_many :comments, dependent: :destroy
  has_many :lessons, foreign_key: :created_by
  has_many :qualifications, dependent: :destroy
  has_many :videos, dependent: :destroy

  has_and_belongs_to_many :specializations

  validates_uniqueness_of :email

And Specialization model is

class Specialization < ApplicationRecord
  has_and_belongs_to_many :users

end

And join table is

class JoinTableUserSpecialization < ActiveRecord::Migration[5.0]


def change
    create_join_table :users, :specializations do |t|
      t.index [:user_id, :specialization_id], name: "index_users_on_specialization_id"
      t.index [:specialization_id, :user_id], name: "index_specializations_on_user_id"
    end
  end
end

Specialization controller is

def suggest
    @specializations = Specialization.all
  end
halfer
  • 19,824
  • 17
  • 99
  • 186
Thananjaya S
  • 1,451
  • 4
  • 18
  • 31

1 Answers1

0
def suggest
 @specializations = Specialization.where.not(id: current_user.specializations)
end
krishnar
  • 2,537
  • 9
  • 23
  • Hi krishnar. Would you add a sentence or two before this code sample to explain why this is the solution? We tend to discourage code-only answers, since we think the presentation of some reasoning may be useful for future readers. Thanks! – halfer Oct 20 '17 at 17:53