我的联接表不会从源自它的两个表中自动填充。我花了大约5个小时尝试以各种方式连接它无济于事。我的代码的任何指导将非常感激。
这是我的代码。
第一项:
`class Order < ActiveRecord::Base
belongs_to :orders_users
has_many :users, :through => :orders_users
validates :item, presence: true
end
def add_order(order)
end
def remove_order(order)
end
`
第二项:
class User < ActiveRecord::Base
belongs_to :orders_users
has_many :orders, :through => :orders_users
validates :username, presence: true, uniqueness: true
has_secure_password
end
迁移:
class CreateJoinTable < ActiveRecord::Migration
def change
create_join_table :users, :orders do |t|
t.index [:user_id, :order_id]
t.index [:order_id, :user_id]
end
end
end
订单控制器:
class OrdersController < ApplicationController
def new
binding.pry
@order = Order.new
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to request.referer
else
redirect_to root_path
end
end
def show
@order
end
private
def order_params
params.require(:order).permit(:item)
end
end
用户控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:current_user_id] = @user.id
redirect_to user_path(@user)
else
redirect_to new_user_path(@user)
end
end
def show
@user = User.find(params[:id])
end
def destroy
@user = User.find(params[:id])
session[:current_user_id] = nil
@user.destroy
redirect_to root_path
end
def index
@user
end
private
def user_params
params.require(:user).permit(:username, :password,:password_confirmation)
end
end
答案 0 :(得分:1)
您已将Order
和User
模型上的关联混合在一起。
您有2个选项。
选项1
将您的User
和Order
模型更改为:
class Order < ActiveRecord::Base
has_and_belongs_to_many :users
validates :item, presence: true
def add_order(order)
end
def remove_order(order)
end
end
class User < ActiveRecord::Base
has_and_belongs_to_many :orders
validates :username, presence: true, uniqueness: true
has_secure_password
end
选项2 has_many通过
应该是has_many
而不是belongs_to
。
class Order < ActiveRecord::Base
has_many :order_users
has_many :users, :through => :order_users
validates :item, presence: true
def add_order(order)
end
def remove_order(order)
end
end
class User < ActiveRecord::Base
has_many :order_users
has_many :orders, :through => :order_users
validates :username, presence: true, uniqueness: true
has_secure_password
end
回滚db以删除连接表
rake db:rollback
使用JoinTable
迁移删除旧的迁移文件。
创建一个“真实”模型用于“has_many through”
rails g model OrderUser order:references user:references
rake db:migrate
提示如何验证ActiveRecord关系
使用rails控制台,这样您就不必担心控制器正常工作。一个小例子:
rails c
> u = User.create username: "test"
> o = u.orders.new item: "test-item"
> o.save
修改订单控制器
创建订单时,您必须在订单对象上指定current_user。在示例中,我使用current_user
来获取已签名的用户,具体取决于您的应用,您可能需要更改此内容。
class OrdersController < ApplicationController
def new
@order = Order.new
end
def create
@order = Order.new(order_params)
@order.users.build(current_user) #current_user is a function returning the signed in user
if @order.save
redirect_to request.referer
else
redirect_to root_path
end
end
def show
@order
end
private
def order_params
params.require(:order).permit(:item)
end
end
Rails指南中有关has_many through
的一些信息