Ruby on rails Rails中多对多关联的值无法保存在数据库中。
我有用户和角色表,它们具有多对多关联。我的数据库有用户、角色和角色用户表。基本上我想把用户的角色保存在role_users表中,这个表基本上是用户和角色的连接表。我从roles表中获取用户页面上的用户角色,并希望将它们保存在role\u users表中。但我无法在role_users表中保存用户的角色。我已经在用户的“createcontroller”中编写了代码来将数据保存在role_users表中,但这并没有发生 用户\u控制器.rbRuby on rails Rails中多对多关联的值无法保存在数据库中。,ruby-on-rails,ruby,database,Ruby On Rails,Ruby,Database,我有用户和角色表,它们具有多对多关联。我的数据库有用户、角色和角色用户表。基本上我想把用户的角色保存在role_users表中,这个表基本上是用户和角色的连接表。我从roles表中获取用户页面上的用户角色,并希望将它们保存在role\u users表中。但我无法在role_users表中保存用户的角色。我已经在用户的“createcontroller”中编写了代码来将数据保存在role_users表中,但这并没有发生 用户\u控制器.rb class Admin::UsersController
class Admin::UsersController < Admin::BaseController
before_filter :find_user, :only => [:show, :edit, :update, :destroy]
def index
@users = User.all( :order => :email )
@roles = Role.all
end
def show
end
def new
@user = User.new
end
def create
is_admin = params[:user].delete(:is_admin) == "1"
@user = User.new(params[:user])
**@user_role = RoleUser.new({:user_id => @user.id, :role_id => params[:role_user]})**
#@user_role.role_id = params[:role_id]
#@user_role.user_id = current_user.id
@user_role.save
@user.is_admin = is_admin
if @user.save
flash[:notice] = "User has been created."
redirect_to admin_users_path
else
flash[:alert] = "User has not been created."
render :action => :new
end
end
def edit
end
def update
if params[:user][:password].empty?
params[:user].delete(:password)
end
set_admin
if @user.update_attributes(params[:user])
flash[:notice] = "User has been updated."
redirect_to admin_users_path
else
flash[:alert] = "User has not been updated."
render :action => :new
end
end
def destroy
if @user == current_user
flash[:alert] = "You cannot delete yourself!"
else
@user.destroy
flash[:notice] = "User has been deleted."
end
redirect_to admin_users_path
end
private
def find_user
@user = User.find(params[:id])
end
def set_admin
is_admin = params[:user].delete(:is_admin) == "1"
@user.is_admin = true
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130601093644) do
create_table "role_users", :force => true do |t|
t.integer "role_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "team_users", :force => true do |t|
t.integer "team_id"
t.integer "user_id"
t.integer "team_lead_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teams", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "team_lead_id"
end
create_table "users", :force => true 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
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.boolean "is_admin"
t.string "username"
t.string "first_name"
t.string "last_name"
t.string "contact_no"
t.date "birth_date"
t.boolean "is_active"
t.date "joining_date"
t.string "avatar_url"
t.boolean "is_hr"
t.boolean "is_manager"
t.string "designation"
t.string "user_code"
t.string "user_role"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
class User < ActiveRecord::Base
has_many :roles, through: :role_users
has_many :role_users
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation,
:remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
:joining_date, :is_active, :is_hr, :is_manager, :user_code, :designation
# attr_accessible :title, :body
end
class RoleUser < ActiveRecord::Base
belongs_to :user
belongs_to :role
attr_accessible :role_id, :user_id
end
class Role < ActiveRecord::Base
attr_accessible :name
has_many :users, through: :role_users
has_many :role_users
end
\u form.html.erb(用于创建新用户)
但是,数据不会保存在db中。在创建
角色扮演者时,用户实例中还没有id
def create
is_admin = params[:user].delete(:is_admin) == "1"
@user = User.new(params[:user])
# @user is not saved here and has no id
**@user_role = RoleUser.new({:user_id => @user.id, :role_id => params[:role_user]})**
@user_role.save
# based on your validations, it's likely that this @user_role.save is returning false
在与星星的连线上,@user已经生成,但没有保存。保存User
后,尝试构建RoleUser
实例
尝试在以下行后保存角色扮演者
:如果@user.save
在创建角色扮演者
时,您的用户
实例中还没有id
def create
is_admin = params[:user].delete(:is_admin) == "1"
@user = User.new(params[:user])
# @user is not saved here and has no id
**@user_role = RoleUser.new({:user_id => @user.id, :role_id => params[:role_user]})**
@user_role.save
# based on your validations, it's likely that this @user_role.save is returning false
在与星星的连线上,@user已经生成,但没有保存。保存User
后,尝试构建RoleUser
实例
请尝试在这一行之后保存您的RoleUser
:if@user.save
hi@GSP,谢谢您的回复。根据您的建议,我们可以在角色用户表中保存用户id。但仍然没有保存角色_id,它在表中显示nil值。你知道吗?看起来你在控制器中引用了role\u user
作为你的参数,但它的形式不是role\u id
?所以它看起来像:@user\u role=RoleUser.new({:user\u id=>@user.id,:role\u id=>params[:role\u id]})
hi@GSP,谢谢你的回复。根据您的建议,我们可以在角色用户表中保存用户id。但仍然没有保存角色_id,它在表中显示nil值。你知道吗?看起来你在控制器中引用了role\u user
作为你的参数,但它的形式不是role\u id
?所以它看起来像:@user\u role=RoleUser.new({:user\u id=>@user.id,:role\u id=>params[:role\u id]})
def create
is_admin = params[:user].delete(:is_admin) == "1"
@user = User.new(params[:user])
# @user is not saved here and has no id
**@user_role = RoleUser.new({:user_id => @user.id, :role_id => params[:role_user]})**
@user_role.save
# based on your validations, it's likely that this @user_role.save is returning false