Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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 Ruby刷新对象_Ruby On Rails_Ruby_Activerecord_Eager Loading - Fatal编程技术网

Ruby on rails Ruby刷新对象

Ruby on rails Ruby刷新对象,ruby-on-rails,ruby,activerecord,eager-loading,Ruby On Rails,Ruby,Activerecord,Eager Loading,我是RubyonRails的新手,我有一个关于刷新/重新加载关系的问题。在下面的代码段中,我试图在“customer”对象上设置“store”属性,然后让customer对象返回嵌套的store属性,而不是其id。是否有一种方法可以设置store属性,然后告诉RoR“get me the customer and Hydrome the store”,而无需进行另一次急读加载 store_id = params[:id]; store = Store.find( store_id ); sess

我是RubyonRails的新手,我有一个关于刷新/重新加载关系的问题。在下面的代码段中,我试图在“customer”对象上设置“store”属性,然后让customer对象返回嵌套的store属性,而不是其id。是否有一种方法可以设置store属性,然后告诉RoR“get me the customer and Hydrome the store”,而无需进行另一次急读加载

store_id = params[:id];
store = Store.find( store_id );
session[ :store_id ] = store_id;

customer_id = session[ :customer_id ];
customer = Customer.find( customer_id );
customer.store = store;
customer.save

respond_to do |format|
  format.any(:json) { render :json => customer.to_json  }
end

非常感谢您提供的任何帮助或建议,无论它们是否与原始问题相关。

使用params访问json将为您提供以下帮助:

在您的示例中,您只需要更改最后一部分(并且不会在第二次从db加载store对象)


使用params创建json将为您带来以下好处:

在您的示例中,您只需要更改最后一部分(并且不会在第二次从db加载store对象)


在rails 3.1上,您可以通过

customer.association(:store)
在rails的早期版本中,关联对象没有像这样直接公开。然后你可以做像这样的事情

customer.association(:store).loaded?
customer.association(:store).target = store
这不会检查客户和商店是否有关联(即,你可以将任何旧商店粘在那里),因此要小心

我不知道你为什么需要这个-

customer.store = store

不应导致重新加载存储对象或类似的操作。

在rails 3.1上,您可以通过

customer.association(:store)
在rails的早期版本中,关联对象没有像这样直接公开。然后你可以做像这样的事情

customer.association(:store).loaded?
customer.association(:store).target = store
这不会检查客户和商店是否有关联(即,你可以将任何旧商店粘在那里),因此要小心

我不知道你为什么需要这个-

customer.store = store

不应该导致商店对象被重新加载或诸如此类的事情。

你是说
customer.store
只返回商店ID?它应该返回一个
存储
类对象。或者您是在问如何消除使用
customer.store
所需的额外数据库调用?所以您是说
customer.store
只返回存储ID?它应该返回一个
存储
类对象。或者你是在问如何消除使用
customer.store
需要的额外数据库调用?过来聊天!!!我有一些有趣的东西给你@阿隆尼,谢谢,阿隆尼,这就成功了。哦,你在家吗?很抱歉,我刚才吃得太多了。:)很高兴我能帮助你,我叫阿隆尼;)过来聊天!!!我有一些有趣的东西给你@阿隆尼,谢谢,阿隆尼,这就成功了。哦,你在家吗?很抱歉,我刚才吃得太多了。:)很高兴我能帮助你,我叫阿隆尼;)谢谢你的帮助,弗雷德里克。我不得不告诉json使用Alony的建议序列化出我的子对象。感谢Frederick的帮助。我不得不告诉json使用Alony的建议序列化我的子对象。