我有两个模型通过关联加入了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控制台执行相同的查询时,创建这种方式的组织似乎会返回用户。所以布线是正确的,但我的形式或params在某种程度上一定有问题吗?
这就是我通过种子数据成功创建了一个与之关联的用户的组织:
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
为什么会这样?关系只能通过形式起作用我在这里误解了什么吗?
答案 0 :(得分:2)
您无法在控制器的create
方法中创建关系。你现在有:
@organization = Organization.new(organization_params)
您应该创建如何在种子文件中创建它:
@user = #find your user here
@organization = @user.organizations.new(organization_params)
这应该创建您的组织和关系。
答案 1 :(得分:0)
您可能只想手动创建连接表。这不是最好的方式,但是既然你被卡住了,也许它会帮助你至少让车轮滚动。
@organization = Organization.new(organization_params)
OrganizationsUser.create(
user_id: current_user.id, organization_id: @organization.id
)
答案 2 :(得分:0)
我这样解决了这个问题:
u = current_user
o = u.organizations.build(name:'blah')
u.save
o.save
由于某种原因,必须保存两个对象以保存关系。不知道为什么。
控制器中有这样的东西:
def create
@user = current_user
@organization = @user.organizations.build(organization_params)
respond_to do |format|
if @user.save && @organization.save