是否可以仅在内存中操作 has_many ?

发布于 2024-10-20 07:30:56 字数 960 浏览 2 评论 0原文

平台: Rails 3

要求:

一个表单 (businesses#edit),允许用户向 Business 提交更新code> 记录 (businesses#update。) 提交后,businesses#update 不会将更改保留到数据库,而是发送一封包含新信息的电子邮件以供人工审核。

如何完成:

加载Business模型,更新内存中的属性,将其传递给ActionMailer,完成时无需保存模型。

问题

Business 模型有一个 has_many :business_categories :through :business_categories_businesses ,它是一个 has_many :business_categories_businesses 并且每当我操纵 Business.business_categories (例如,@business.business_categories = BusinessCategory.where(:id => params[:business][:business_categories])。)更改会立即保存到数据库中。

我找不到任何方法来操作内存中的集合,以便将其传递给操作邮件程序。在短期内,我将通过不执行作业来破解它,并将新的 BusinessCategories 集合传递给 ActionMailer 来自行解决,但现在这只是让我烦恼,我推迟了大众的集体智慧:

仅在内存中操纵 has_many 关系是可能的,如果可以的话,如何操作?请保佑我的理智。

先感谢您!

Platform: Rails 3

Requirement:

A form (businesses#edit) that allows a user to submit an update to a Business record (businesses#update.) When submitted businesses#update will not persist the changes to the database but will instead send an email with the new information for manual review.

How it's accomplished:

Load the Business model, update the properties in memory, pass it to an ActionMailer, finish without ever saving the model.

Problem:

The Business model has a has_many :business_categories :through :business_categories_businesses which is a has_many :business_categories_businesses and whenever I manipulate the Business.business_categories (@business.business_categories = BusinessCategory.where(:id => params[:business][:business_categories]) for example.) the changes are immediately persisted to the database.

I can not find any way of manipulating that collection in memory only so that it can be passed to an action mailer. In the short-term I'm going to hack it by not doing the assignment and simply pass the new collection of BusinessCategories to the ActionMailer to work out itself, but now this is just annoying me and I defer to the collective wisdom of the crowd:

It is possible to manipulate has_many relations in memory only and if so how? Please save my sanity.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文