0

So after searching long and hard for information that can help us, we've found it difficult to find an answer we can work with.

Our problem is that we have two tables joined through a HABTM relationship called Schools and Organizations. A school is created first, and then an Organization takes the list of schools, allows the user to select one, and then populates a third table OrganizationsSchools with both the school_id and the organization_id.

Models for the three are as follows: Schools model:

class School < ActiveRecord::Base
  has_and_belongs_to_many :organizations, :join_table => 'organizations_schools'
  attr_accessible :name
  validates :name, :presence => true
end

Organizations model:

class Organization < ActiveRecord::Base
  has_many :materials
  has_many :users
  has_and_belongs_to_many :causes
  has_and_belongs_to_many :schools, :join_table => 'organizations_schools'
  attr_accessible :name, :unlogged_books_num, :id

  validates :name, :presence => true
end

The form for Organizations:

<%= form_for(@organization) do |f| %>
  <% if @organization.errors.any? %>
     <div id="error_explanation">
      <h2><%= pluralize(@organization.errors.count, "error") %> prohibited this organization from being saved:</h2>

      <ul>
      <% @organization.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


<% @schools = School.all %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :unlogged_books_num %><br />
    <%= f.number_field :unlogged_books_num %>
  </div>
  <div class="field">
    <%= f.label 'School' %><br />
    <% school_id = nil %>
    <%= collection_select(nil, school_id, @schools, :id, :name) %>  
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And finally, the create function in our Organizations controller

class OrganizationsController < ApplicationController
  .
  .
  .
  def create
    @organization = Organization.new(params[:organization])
    org_id = @organization.id
    school_id = @organization.school_id
    @org_school = OrganizationsSchool.create(:organization_id => org_id, :school_id => school_id)

    respond_to do |format|
      if @organization.save
        format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
        format.json { render json: @organization, status: :created, location: @organization }
      else
        format.html { render action: "new" }
        format.json { render json: @organization.errors, status: :unprocessable_entity }
      end
    end
  end
end

All other functions in our controller are restfully created. Please also know that I'm not the greatest with databases, and though I'm familiar with rails, I'm not by any stretch of the word proficient at using it.

1 Answers1

4

In a has_and_belongs_to_many, there wouldn't be a school_id field for Organization. It sounds like you actually want:

class Organization < ActiveRecord::Base
  belongs_to :school
  ...
end

class School < ActiveRecord::Base
  has_many :organizations
end

if you really want HABTM, then you could write:

@organization.schools << School.find school_id

EDIT: Obviously your controller code would need additional changes if you swtiched to a has_many relationship

Joshua Scott
  • 645
  • 5
  • 9
  • The problem with having a belongs_to and a has_many, is that we have organizations that are repeated across different schools, but a school can have many different organizations as well. I was trying to see how we might connect the form to the controller, and that's why the controller is currently written as it is. Thank you for the timely reply btw – CimmerianMuse Jun 11 '13 at 21:42