【发布时间】:2015-12-13 08:50:24
【问题描述】:
我有两个模型通过关联加入了 has_many:
这是表格:
create_table "organizations", force: :cascade do |t|
t.string "name"
t.string "description"
t.string "mission"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "organizations_users", force: :cascade do |t|
t.integer "user_id"
t.integer "organization_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
end
以下是模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :organizations_users
has_many :organizations, :through => :organizations_users
end
class Organization < ActiveRecord::Base
has_many :organizations_users
has_many :users, :through => :organizations_users
has_many :categories, as: :categorizable
end
class OrganizationsUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
end
当我通过我的表单创建一个新组织时,它会充分创建。但是,由于某种原因,通过我的表单提交时,关系没有得到保存。
例如,如果我这样做:
o = Organization.last
o.users
我得到一个空的查询集:
User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "organizations_users" ON "users"."id" = "organizations_users"."user_id" WHERE "organizations_users"."organization_id" = $1 [["organization_id", 11]]
=> #<ActiveRecord::Associations::CollectionProxy []>
这是我的表格:
<%= 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 |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class:"form-control", autofocus: true %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class:"form-control", autofocus: true %>
</div>
<div class="form-group">
<%= f.label :mission %><br>
<%= f.text_field :mission, class:"form-control", autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Submit", class:"btn btn-primary" %>
</div>
<% end %>
这是我的控制器:
class OrganizationsController < ApplicationController
before_action :set_organization, only: [:show, :edit, :update, :destroy]
# GET /organizations
# GET /organizations.json
def index
@organizations = Organization.all.paginate(:page => params[:page], :per_page => 10)
end
# GET /organizations/1
# GET /organizations/1.json
def show
end
# GET /organizations/new
def new
@organization = Organization.new
end
# GET /organizations/1/edit
def edit
end
# POST /organizations
# POST /organizations.json
def create
@organization = Organization.new(organization_params)
respond_to do |format|
if @organization.save
format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
format.json { render :show, status: :created, location: @organization }
else
format.html { render :new }
format.json { render json: @organization.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /organizations/1
# PATCH/PUT /organizations/1.json
def update
respond_to do |format|
if @organization.update(organization_params)
format.html { redirect_to @organization, notice: 'Organization was successfully updated.' }
format.json { render :show, status: :ok, location: @organization }
else
format.html { render :edit }
format.json { render json: @organization.errors, status: :unprocessable_entity }
end
end
end
# DELETE /organizations/1
# DELETE /organizations/1.json
def destroy
@organization.destroy
respond_to do |format|
format.html { redirect_to organizations_url, notice: 'Organization was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_organization
@organization = Organization.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def organization_params
params.require(:organization).permit!
end
end
奇怪的是,我使用 Faker gem 创建了一些种子数据,当我通过我的 rails 控制台执行相同的查询时,以这种方式创建的组织似乎返回了一个用户。所以接线是正确的,但我的表单或参数一定有问题?
这就是我成功创建组织的方式,该组织通过种子数据关联了一个用户:
for n in 1..10 do
user = User.create!(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
email: Faker::Internet.email,
password: "password",
employer: Faker::University.name,
title: Faker::Name.title,
short_bio: Faker::Lorem.paragraph)
user.organizations.create!(
name: Faker::Company.name,
description: Faker::Lorem.paragraph,
mission: Faker::Lorem.sentence)
end
为什么会这样?关系仅通过表格不起作用我在这里误解了什么吗?
【问题讨论】:
-
也许这会有所帮助:
autosave: true选项呢?如果没有此选项,您必须在每次更新关系时显式保存对象。
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord model-view-controller