Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 在Rails 4中,有一个属于关系问题吗_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 在Rails 4中,有一个属于关系问题吗

Ruby on rails 在Rails 4中,有一个属于关系问题吗,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,我对Rails 4中的关系有问题 有4种型号 使用者 请求 制造 模型 一个用户有多个请求,一个请求有一个Make,一个Make有多个模型 用户->Requests和Make->Models有很多关系都很好,但是Request->Make有一个关系失败了 class Request < ActiveRecord::Base belongs_to :user has_one :make end class Make < ActiveRecord::Base has

我对Rails 4中的关系有问题

有4种型号

  • 使用者
  • 请求
  • 制造
  • 模型
  • 一个用户有多个请求,一个请求有一个Make,一个Make有多个模型

    用户->Requests和Make->Models有很多关系都很好,但是Request->Make有一个关系失败了

    class Request < ActiveRecord::Base
        belongs_to :user
        has_one :make
    end
    
    class Make < ActiveRecord::Base
      has_many :models
      belongs_to :request
    end
    
    当我尝试将Make分配给请求时,会出现以下错误

    Mysql2::Error: Unknown column 'makes.request_id' in 'where clause': SELECT  `makes`.* FROM `makes`  WHERE `makes`.`request_id` = 7 LIMIT 1                                                                           
    ActiveRecord::StatementInvalid Exception: Mysql2::Error: Unknown column 'makes.request_id' in 'where clause': SELECT  `makes`.* FROM `makes`  WHERE `makes`.`request_id` = 7 LIMIT 1                                 
    nil  
    

    为什么ActiveRecord在Make中需要请求\u id?这难道不是只适用于像我在User->Requests and Makes->Models relationships中那样有很多关系吗

    根据您的架构和错误消息,在
    makes
    中的
    请求中似乎没有外键

    例如,请参见:


    在这里,供应商有一个帐户,
    帐户
    有一个
    供应商id
    。查看相应的迁移示例。

    具有_one
    属于_to
    方法允许您在模型之间创建一对一关联,因此允许您通过各种辅助方法轻松访问彼此

    只有当另一个类包含“外键”时,才应使用
    has_one
    。如果当前类包含“外键”,则应使用
    所属\u to

    根据您提供的模式,应该像下面的示例中那样定义关联:

    class Request < ActiveRecord::Base
      # Because you have a `make_id` column in the "requests" table.
      belongs_to :make
    end
    
    class Make < ActiveRecord::Base
      # Because the Request model has the "foreign key" that 
      # creates the association, in this case `make_id`.
      has_one :request
    end
    
    我们使用
    has_one
    own_to
    方法来创建关联,但也可以访问Rails为我们创建的所有帮助器方法。根据您是想从请求访问Make,还是以其他方式访问,您可以从特定类中添加或删除has\u one所属的

    但是,通过将每一个方法都保留在两个类中,我们可以从两个类中访问一组helper方法。例如,如果您从请求中删除了
    所属的:make
    ,您将无法使用
    @Request.make
    从请求中访问make。但是,只要保持
    @make.Request
    有一个方法,您仍然可以使用
    @make.Request
    访问make的请求

    请记住,迁移是它们自己的事情,我们还需要通过将“外键”添加到正确的表中来在数据库级别设置关联。我们可以通过查看has_onebelown_to的定义,轻松找出在何处添加外键has_one表示“外键”应该在关联的表中,所属表示它应该在该表中

    我希望这有帮助!这里有更多信息:


    您可能只需要将request\u id列添加到makes表中

    运行:


    谢谢你的帮助!你是对的,因为协会走错了路。我一直在寻找一对多的关系,所以我有很多在制造方面,而不是一对一。
    class Request < ActiveRecord::Base
      # Because you have a `make_id` column in the "requests" table.
      belongs_to :make
    end
    
    class Make < ActiveRecord::Base
      # Because the Request model has the "foreign key" that 
      # creates the association, in this case `make_id`.
      has_one :request
    end
    
    # Request Model
    @request = Request.create!
    
    # Helper methods to access `make`: make, build_make, create_make, make=
    @request.make
    @request.build_make
    @request.create_make
    @request.make = Make.create!
    # ...
    
    
    # Make Model
    @make = Make.create!
    
    # Helper methods to access `request`
    @make.request
    @make.build_request
    @make.create_request
    @make.request = Request.create!
    #...
    
    rails g migration add_request_id_to_makes request_id:integer
    
    
    bundle exec rake db:migrate