Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails与多对多模型的关联_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails RubyonRails与多对多模型的关联

Ruby on rails RubyonRails与多对多模型的关联,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,Ruby On Rails 4,我是RoR的初学者,正在尝试处理我的许多联想 我处理了我的模型和迁移之间的关系 class CreateRelationships < ActiveRecord::Migration def change create_table :relationships do |t| t.belongs_to :user t.belongs_to :trip t.timestamps end add_index :relationships, :user_id add_ind

我是RoR的初学者,正在尝试处理我的许多联想

我处理了我的模型和迁移之间的关系

class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
    t.belongs_to :user
    t.belongs_to :trip
    t.timestamps
end
add_index :relationships, :user_id
add_index :relationships, :trip_id
end
end
出行模型

class Trip < ActiveRecord::Base
has_many :relationships
has_many :topics, through: :relationships
end
class Trip < ActiveRecord::Base
has_many :relationships
has_many :users, through: :relationships
end
classtrip
用户模型

class User < ActiveRecord::Base
has_many :relationships
has_many :trips, through: :relationships
end
class User < ActiveRecord::Base
has_many :relationships
has_many :trips, through: :relationships
end
class用户
关系模型

class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :trip
end
类关系
我的迁移

class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
    t.belongs_to :user
    t.belongs_to :trip
    t.timestamps
end
add_index :relationships, :user_id
add_index :relationships, :trip_id
end
end
class CreateRelationships
用户控制器

 class UsersController < ApplicationController


 before_action :set_user, only: [:show, :edit, :update, :destroy]



def trips
@user = User.find(params[:id])
 @trips = @user.trips

end
def index
@users = User.all
end

  def show
@user = User.find(params[:id])
end
def new
@user = User.new
@trips = Trip.all
end
def edit
end
def create
@user = User.new(user_params)

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



def update
respond_to do |format|
  if @user.update(user_params)
    format.html { redirect_to @user, notice: 'User was successfully updated.' }
    format.json { render :show, status: :ok, location: @user }
  else
    format.html { render :edit }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end
end


def destroy
@user.destroy
respond_to do |format|
  format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
  format.json { head :no_content }
 end
 end

private

def set_user
  @user = User.find(params[:id])
end


def user_params
  params.require(:user).permit(:name)
end
end
class UsersController
我不知道如何将行程分配给用户,也不知道在控制器或表单中将分配写入何处

@user.trips在模式中更改为:--

出行模型

class Trip < ActiveRecord::Base
has_many :relationships
has_many :topics, through: :relationships
end
class Trip < ActiveRecord::Base
has_many :relationships
has_many :users, through: :relationships
end
classtrip
用户模型

class User < ActiveRecord::Base
has_many :relationships
has_many :trips, through: :relationships
end
class User < ActiveRecord::Base
has_many :relationships
has_many :trips, through: :relationships
end
class用户
然后试着这样做:-

@user.trips << @trip
@user.trips