mardi 8 mars 2016

Adding Association to Join Table for HaBtM Relationship

I currently have 3 models (user, pairing, meetings), and the join table meetings_pairings.

User.rb

class User < ActiveRecord::Base

  enum role: [:student, :supervisor, :admin]

  has_many :students, class_name: "User",
                      foreign_key: "supervisor_id"

  belongs_to :supervisor, class_name: "User"

  has_and_belongs_to_many :pairings

end

Pairings.rb

class Pairing < ActiveRecord::Base

  has_and_belongs_to_many :supervisor, class_name: 'User'
  belongs_to :student, class_name: 'User'

  has_and_belongs_to_many :meetings, join_table: :meetings_pairings

end

Meetings.rb

class Meeting < ActiveRecord::Base

    enum status: [:available, :unavailable]

    has_and_belongs_to_many :pairings, join_table: :meetings_pairings

end

Schema.rb (relevant bits)

  create_table "meetings", force: :cascade do |t|
    t.date     "meeting_date"
    t.datetime "meeting_time"
    t.integer  "status",       default: 0, null: false
    t.boolean  "accepted"
  end

  create_table "meetings_pairings", id: false, force: :cascade do |t|
    t.integer "pairing_id"
    t.integer "meeting_id"
  end

  add_index "meetings_pairings", ["meeting_id"], name: "index_meetings_pairings_on_meeting_id"
  add_index "meetings_pairings", ["pairing_id"], name: "index_meetings_pairings_on_pairing_id"

  create_table "pairings", force: :cascade do |t|
    t.integer  "supervisor_id"
    t.integer  "student_id"
    t.string   "project_title"
  end

  add_index "pairings", ["student_id"], name: "index_pairings_on_student_id", unique: true
  add_index "pairings", ["supervisor_id"], name: "index_pairings_on_supervisor_id"

I created the view and controller to enable a user (supervisor, which is in a pairing) to create a meeting. However I don't know how to add this association to the join table.

meetings_controller.rb

class MeetingsController < ApplicationController

    def index
        @meetings = Meeting.all
    end

    def new
        @meeting = Meeting.new
    end

    def create
        @meeting = Meeting.new(meeting_params)
        if @meeting.save
      redirect_to meetings_path, :notice => "Meeting Created!"
    else 
      redirect_to meetings_path, :notice => "Meeting Failed!"
    end
  end

  def show
    @meeting = Meeting.find(params[:id])
  end  

  private
  def meeting_params
    params.require(:meeting).permit(:meeting_date, :meeting_time)
  end

end

Form from the view

<%= form_for @meeting do |f| %>
  <div class="field">
    <%= f.label :meeting_date %><br />
    <%= f.text_field :meeting_date %>
  </div>
  <div class="field">
    <%= f.label :meeting_time %><br />
    <%= f.time_select :meeting_time, :ampm => true, :minute_step => 30, :default => {:hour => '9', :minute => '0'} %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This creates an entry in the meetings table, so my question is how can I make it so that an entry is made into the join table with the pairing id of the current user who created the meeting ?

Aucun commentaire:

Enregistrer un commentaire