Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 4的活动记录中,该元素属于关联_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 在RubyonRails 4的活动记录中,该元素属于关联

Ruby on rails 在RubyonRails 4的活动记录中,该元素属于关联,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有问题。我需要知道是否存在使用以下关联的某种方式,以便以更简单的方式从一个类导航到另一个类 SCHEMA ActiveRecord::Schema.define(version: 20140712054858) do create_table "customers", force: true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end crea

我有问题。我需要知道是否存在使用以下关联的某种方式,以便以更简单的方式从一个类导航到另一个类

SCHEMA

ActiveRecord::Schema.define(version: 20140712054858) do

  create_table "customers", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "orders", force: true do |t|
    t.integer  "customer_id"
    t.datetime "order_date"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
customer.rb

class Customer < ActiveRecord::Base
end
class客户
order.rb

class Order < ActiveRecord::Base
     belongs_to :customer
   end
类顺序
我的使用方法

  2.0.0-p481 :044 > @customer1=Customer.create(:name=>"John")
   (0.2ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "customers" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", Sat, 12 Jul 2014 06:18:24 UTC +00:00], ["name", "John"], ["updated_at", Sat, 12 Jul 2014 06:18:24 UTC +00:00]]
   (162.3ms)  commit transaction
 => #<Customer id: 4, name: "John", created_at: "2014-07-12 06:18:24", updated_at: "2014-07-12 06:18:24"> 
2.0.0-p481 :045 > @order1=Order.new
 => #<Order id: nil, customer_id: nil, order_date: nil, created_at: nil, updated_at: nil> 
2.0.0-p481 :046 > @order1.order_date=Time.now
 => 2014-07-12 03:19:31 -0300 
2.0.0-p481 :047 > @order1.customer_id=@customer.id
 => 1 
2.0.0-p481 :048 > @order1.save
   (0.2ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "orders" ("created_at", "customer_id", "order_date", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sat, 12 Jul 2014 06:20:03 UTC +00:00], ["customer_id", 1], ["order_date", Sat, 12 Jul 2014 06:19:31 UTC +00:00], ["updated_at", Sat, 12 Jul 2014 06:20:03 UTC +00:00]]
   (171.1ms)  commit transaction
 => true 
2.0.0-p481:044>@customer1=Customer.create(:name=>“John”)
(0.2ms)开始事务处理
SQL(0.6ms)插入“客户”(“创建时间”,“名称”,“更新时间”)值(?,,?)[[“创建时间”,2014年7月12日周六06:18:24 UTC+00:00],“名称”,“约翰”],[“更新时间”,2014年7月12日周六06:18:24 UTC+00:00]]
(162.3ms)提交事务
=> # 
2.0.0-p481:045>@order1=Order.new
=> # 
2.0.0-p481:046>@order1.order\u date=Time.now
=> 2014-07-12 03:19:31 -0300 
2.0.0-p481:047>@order1.customer\u id=@customer.id
=> 1 
2.0.0-p481:048>@order1.save
(0.2ms)开始事务处理
SQL(0.7ms)插入“订单”(“创建时间”、“客户id”、“订单日期”、“更新时间”)值(?,,?,?)[[“创建时间”,2014年7月12日周六06:20:03 UTC+00:00],“客户id”,1],“订单日期”,2014年7月12日周六06:19:31 UTC+00:00],“更新时间”,2014年7月12日周六06:20:03 UTC+00:00]]
(171.1ms)提交事务
=>正确

不存在使@customer1@order1和自动映射的某种方式?

您可以从
Customer
设置反向关联,如下所示:

class Customer < ActiveRecord::Base
  has_many :orders
end

通过在
Customer
中说
有很多:订单,Rails假定
Order
表有一个
Customer\u id
,在您的情况下,这是现成的。

关联

您当然需要查看您的-您的
所属
需要与一个
has\u many
匹配:

因此,您的模型将如下所示:

#app/models/customer.rb
Class Customer < ActiveRecord::Base
    has_many :orders
end

#app/models/order.rb
Class Order < ActiveRecord::Base
    belongs_to :customer
end
#app/models/customer.rb
Class Customer < ActiveRecord::Base
    has_many :orders
end

#app/models/order.rb
Class Order < ActiveRecord::Base
    belongs_to :customer
end
#app/controllers/customers_controller.rb
Class CustomersController < ApplicationController
   def show
      @customer = Customer.find params[:id]
      @orders = @customer.orders
   end
end