Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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 ActiveRecord如何将记录链接在一起?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord如何将记录链接在一起?

Ruby on rails ActiveRecord如何将记录链接在一起?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,因此,很难找到一个快速的答案。我在纯控制台应用程序中使用ActiveRecord,我的类设置如下: class Owner < ActiveRecord::Base has_many :profiles end class Profile < ActiveRecord::Base has_many :lines belongs_to :owner end class Line < ActiveRecord::Base belongs_to :profiles end AR

因此,很难找到一个快速的答案。我在纯控制台应用程序中使用ActiveRecord,我的类设置如下:

class Owner < ActiveRecord::Base
has_many :profiles
end

class Profile < ActiveRecord::Base
has_many :lines
belongs_to :owner
end

class Line < ActiveRecord::Base
belongs_to :profiles
end

AR如何知道将此配置文件实例链接到所有者?数据库中已经存在的特定所有者呢?我怎么说呢?如何一次完成所有链接?(行、个人资料、所有者)?

阅读本文以了解基本知识

那有点没用。假设我正在同时创建一条线、一个概要文件和一个所有者。根据指南判断,我会做一些类似Line.Profiles.Owners.create的事情?如果这些表中的列名称相同怎么办?指南的哪一部分会这样说?我建议您真正从基础知识开始,了解Rails的ActiveRecord。但我会尽力回答你的问题。如果您想同时创建不同的模型,只需执行
Line.create!(参数[:行]
所有者。创建。但是,如果您正在研究如何关联不同的模型,例如:
@profile=@owner.profiles.create!(params[:profile])
相关部分位于顶部附近,它们解释了如何在数据库类中设置关系。。通过此更改,为特定客户创建新订单会更容易(等),但请注意大写字母。由于
客户
有许多
订单
,因此客户实例
@Customer
,由于声明的
客户
模型中有许多:订单
,使用上面提到的语法,可以创建
订单
,并同时将其与
@customer
关联。谢谢:)您的措辞终于让我明白了。
Profile.create( :thing => "thing", :otherthing => "otherthing" )