与 mongoid 包装器的多重关系
我有以下问题:
我有 3 个模型订单、用户和应用程序,具有以下关系:
用户:
has_many :apps
has_many :orders
订单:
belongs_to :user
has_one :app
应用程序:
belongs_to :user
belongs_to :app
基于这些模型,我想使用 mongoid(rails mongodb-wrapper)进行以下查询:
@order = current_user.orders.new(...)
@app = @order.build_app()
结果应该是,创建一个新的应用程序和订单,其中订单具有 app_id 和 user_id,而新应用程序仅获取 user_id。它实际上所做的是,它创建一个新订单和新应用程序,但只有订单中包含 user_id,新应用程序保持不变。
有什么建议吗?谢谢!
i have following problem:
i have 3 models orders, users and apps, with following relations:
Users:
has_many :apps
has_many :orders
Orders:
belongs_to :user
has_one :app
Apps:
belongs_to :user
belongs_to :app
based on these models, i want to make the following queries with mongoid (rails mongodb-wrapper):
@order = current_user.orders.new(...)
@app = @order.build_app()
the result should be, that an new app and order is created, where the order has an app_id and an user_id and the new app gets just a user_id. what it actually does is, that it creates a new order and new app, but only with an user_id in the order, the new app stays untouched.
any advice? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
新应用程序应具有 order_id,但不具有您编码方式的用户 ID。另外,如果您希望它们持续存在,您需要创建而不是构建。
唯一要设置的 id 是那些属于用于构建/创建的关系的一部分的 id。其他任何东西都需要传入。
The new app should have an order_id but not a user id with the way you coded it. Also, if you want them to persist you need to create instead of build.
The only ids that will be set are those that are part of the relation used for the build/create. Anything else needs to be passed in.