Ruby on rails 如何为HABTM关系Rails 3、postgresql填充联接表

Ruby on rails 如何为HABTM关系Rails 3、postgresql填充联接表,ruby-on-rails,ruby-on-rails-3,postgresql,has-and-belongs-to-many,Ruby On Rails,Ruby On Rails 3,Postgresql,Has And Belongs To Many,因此,在经过长时间的努力寻找能够帮助我们的信息之后,我们发现很难找到一个我们可以合作的答案 我们的问题是,我们有两个表通过一个名为“学校和组织”的HABTM关系连接在一起。首先创建一个学校,然后组织获取学校列表,允许用户选择一个,然后用学校id和组织id填充第三个表OrganizationsChools 这三个项目的模式如下: 学校模式: class School < ActiveRecord::Base has_and_belongs_to_many :organizations,

因此,在经过长时间的努力寻找能够帮助我们的信息之后,我们发现很难找到一个我们可以合作的答案

我们的问题是,我们有两个表通过一个名为“学校和组织”的HABTM关系连接在一起。首先创建一个学校,然后组织获取学校列表,允许用户选择一个,然后用学校id和组织id填充第三个表OrganizationsChools

这三个项目的模式如下: 学校模式:

class School < ActiveRecord::Base
  has_and_belongs_to_many :organizations, :join_table => 'organizations_schools'
  attr_accessible :name
  validates :name, :presence => true
end
班级学校“组织学校”
可访问属性:名称
验证:name,:presence=>true
结束
组织模式:

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
类组织“组织学校”
属性可访问性:名称,:未标记的图书数量,:id
验证:name,:presence=>true
结束
各组织的表格:

<%= 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 %>

禁止保存此组织:



最后,我们的组织控制器中的create函数

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
class OrganizationsControllerorg\u id,:school\u id=>school\u id)
回应待办事项|格式|
如果@organization.save
format.html{将_重定向到@organization,注意:“组织已成功创建”。}
format.json{render json:@organization,status::created,location:@organization}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@organization.errors,status::unprocessable_entity}
结束
结束
结束
结束
控制器中的所有其他函数都是RESTfull创建的。
还请知道,我不是最擅长使用数据库的人,尽管我熟悉rails,但我并不擅长使用它

在一个“拥有”和“属于”很多人的组织中,就不会有一个“学校id”字段。听起来你真的想要:

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

class School < ActiveRecord::Base
  has_many :organizations
end
类组织
如果你真的想要HABTM,那么你可以写:


@organization.schools在一个has\u和\u属于\u many的组织中,组织不会有一个school\u id字段。听起来你真的想要:

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

class School < ActiveRecord::Base
  has_many :organizations
end
类组织
如果你真的想要HABTM,那么你可以写:


@organization.schools拥有一个“属于”和一个“拥有”多个组织的问题在于,我们的组织在不同的学校中重复出现,但一所学校也可以拥有许多不同的组织。我试图看看我们如何将表单连接到控制器,这就是为什么控制器目前是按原样编写的。感谢您及时的回复。拥有一个“属于”和“拥有多个”的问题在于,我们的组织在不同的学校中重复出现,但一所学校也可以有许多不同的组织。我试图看看我们如何将表单连接到控制器,这就是为什么控制器目前是按原样编写的。谢谢你及时回复