Ruby on rails 协会不';无法在两个型号之间保存

Ruby on rails 协会不';无法在两个型号之间保存,ruby-on-rails,associations,ruby-on-rails-5,Ruby On Rails,Associations,Ruby On Rails 5,使用Rails5,当我试图保存这两个模型(用户和提供者)之间的关联时,我遇到了一个问题 我的型号: class User < ApplicationRecord # Relations has_and_belongs_to_many :providers end class Provider < ApplicationRecord # Relations has_and_belongs_to_many :users accepts_nested_attri

使用Rails5,当我试图保存这两个模型(用户和提供者)之间的关联时,我遇到了一个问题

我的型号:

class User < ApplicationRecord
   # Relations
   has_and_belongs_to_many :providers
end


class Provider < ApplicationRecord
  # Relations
  has_and_belongs_to_many :users
  accepts_nested_attributes_for :users
end
class ProvidersController < ApplicationController
  def new
    @provider = current_user.providers.new
  end

  def create
    @provider = current_user.providers.new(provider_params)
    if @provider.save
      redirect_to root_path
    else
      render :new
    end
  end

  private

    def provider_params
      params[:provider][:status] = 'name'
      params.require(:provider).permit(:name, :status)
    end
= simple_form_for @provider do |f|
  = f.error_notification
  = f.input :name, required: false
  = f.button :submit
在“创建”操作中,将创建一个新的提供程序,但它未链接到当前用户。(联接表中未插入任何数据)

我不知道我为什么会有这种行为。 在控制台中,如果执行以下操作:

@user = User.create(email: "user@test.com", password: "password")
@provider = @user.providers.create(name: "Provider1", status: "name")
@provider.save
然后正确保存关联

 > @user.providers
 => #<ActiveRecord::Associations::CollectionProxy [#<Provider id: 17, name: "Provider1", status: "name", created_at: "2017-07-25 09:37:19", updated_at: "2017-07-25 09:37:19">]>
这是日志

  ÈStarted POST "/providers" for 127.0.0.1 at 2017-07-25 11:49:27 +0200
  Processing by ProvidersController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"27O2Tz4bbqhfRmcuq+0DIZMebSaYVc6IO/uy889Z48fF1l3c8GfIZ+WcQvZKfUeEIB5+YbrM9dON2RH47p3TIQ==", "provider"=>{"name"=>"My new provider"}, "commit"=>"Sauvegarder"}
    User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 5], ["LIMIT", 1]]
     (0.2ms)  BEGIN
    SQL (0.4ms)  INSERT INTO "providers" ("name", "status", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "My new provider"], ["status", "name"], ["created_at", "2017-07-25 09:49:27.837165"], ["updated_at", "2017-07-25 09:49:27.837165"]]
     (4.0ms)  COMMIT
  Redirected to http://localhost:3000/providers/18/steps.location
  Completed 302 Found in 12ms (ActiveRecord: 5.0ms)


  Started GET "/providers/18/steps.location" for 127.0.0.1 at 2017-07-25 11:49:27 +0200
  Processing by Providers::StepsController#index as 
    Parameters: {"provider_id"=>"18"}
  Redirected to http://localhost:3000/providers/18/steps/registration
  Completed 302 Found in 2ms (ActiveRecord: 0.0ms)


  Started GET "/providers/18/steps/registration" for 127.0.0.1 at 2017-07-25 11:49:27 +0200
  Processing by Providers::StepsController#show as HTML
    Parameters: {"provider_id"=>"18", "id"=>"registration"}
    Provider Load (0.3ms)  SELECT  "providers".* FROM "providers" WHERE "providers"."id" = $1 LIMIT $2  [["id", 18], ["LIMIT", 1]]
    Rendering providers/steps/registration.html.haml within layouts/provider
    User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "providers_users" ON "users"."id" = "providers_users"."user_id" WHERE "providers_users"."provider_id" = $1  [["provider_id", 18]]
    Rendered providers/steps/registration.html.haml within layouts/provider (13.2ms)
    Rendered shared/_head.html.haml (30.6ms) [cache miss]
    User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 5], ["LIMIT", 1]]
    Rendered shared/_header.html.haml (9.7ms) [cache miss]
  Completed 200 OK in 69ms (Views: 64.7ms | ActiveRecord: 1.1ms)

我知道也许这不是最好的解决办法,但我想到的却是最快的

  def create
    @provider = current_user.providers.create(provider_params)
    if @provider.id
      redirect_to root_path
    else
      render :new
    end
  end
当您使用new only时,这将创建联接表记录为什么它在属于的情况下会工作如果提供程序属于用户并且在迁移中具有用户\u id当前\u user.providers.new将在新实例中添加用户\u id,如果具有并属于多个提供程序,您可以这样做,也许有更好的方法,但这是我首先想到的

或者类似的事情

 def create
    @provider = Provider.new(provider_params)
    if @provider.save
      current_user.providers << @provider
      redirect_to root_path
    else
      render :new
    end
  end
def创建
@provider=provider.new(provider\u参数)
如果@provider.save
当前用户提供程序
我建议使用
has\u many to
关系,而不是
has\u many\u and\u belling\u to
,因为您可以查询联接表,并可能在将来需要时在此联接表中添加更多列


另外,对我来说,这感觉像是Rails的bug/功能改进
@provider.users=[current_user]
不再需要,因为
@provider=current_user.providers.new(provider_参数)
可以推断
@provider
对象已经与
current_user
关联,因此应该已经自动分配。这已经适用于
但有许多
了。似乎只有在HABTM中才会自动分配。

显示触发提交时生成的日志。@Pavan请显示我的日志,重定向有点不同。谢谢等等根据您的表单和控制器,您只为提供商发送输入@Pavan,也就是说我必须在表单上添加一个隐藏字段?我建议查看一下
has\u many to
。这可能是你最好的选择。你想说什么?我在我的项目中尝试了这个解决方案,结果OP期望它在一行提供程序中创建,并加入表记录提供程序和用户。这个解决方案有效,Sedad:)在我看来有点奇怪。如果有更好的解决方案,我会等一会儿。非常感谢@我用不同的方法编辑了我的答案。
 def create
    @provider = Provider.new(provider_params)
    if @provider.save
      current_user.providers << @provider
      redirect_to root_path
    else
      render :new
    end
  end
def create
  @provider = current_user.providers.new(provider_params)
  @provider.users = [current_user]
  if @provider.save
    redirect_to root_path
  else
    render :new
  end
end