← Back to Upcase

How to access attribute on a join table?

How would I access the wildcard_participant attribute on the proxy table for each of the participants below?

How would I set it?

I’ve seen the example here (https://stackoverflow.com/questions/25235025/rails-4-accessing-join-table-attributes) but can’t make it work in Rails 6.

Thank you for any help or insight you may have! Alternatively, if I am going against the framework here, what’s a best practice approach to attributes on the join table (a very common pattern)?

class User < ApplicationRecord
  has_many :participations
  has_many :programs, through: :participations, source: :program
end


class Program < ApplicationRecord
  has_many :participations
  has_many :participants, through: :participations, source: :user
end

class Participation < ApplicationRecord
  belongs_to :program
  belongs_to :user
end

create_table "participations", id: false, force: :cascade do |t|
  t.bigint "user_id", null: false
  t.bigint "program_id", null: false
  t.boolean "wildcard_participant", default: false
  t.index ["program_id", "user_id"], name: "index_participations_on_program_id_and_user_id"
  t.index ["user_id", "program_id"], name: "index_participations_on_user_id_and_program_id"
end


class ParticipantsController < ApplicationController
  ...

  def index
    @participants = current_program.participants
    # how would I access the wildcard_participant attribute on the proxy table for each of the participants?
  end

end