如何访问未持久记录的多对多关联模型属性?

huangapple go评论85阅读模式
英文:

How to access a many-to-many join model attribute for a not persisted record?

问题

Here's the translation of the code-related portion:

  1. 给定以下的结构,其中一个 `Step` 拥有多个 `Items`,而一个 `Item` 拥有多个嵌套的 `Items`
  2. class Step
  3. has_many :step_items
  4. has_many :items, through: :step_items
  5. end
  6. class Item
  7. has_one :step_item
  8. has_one :step, through: :step_item
  9. has_many :nested_items, foreign_key: :parent_item_id
  10. has_many :items, through: :nested_items
  11. end
  12. class StepItem
  13. belongs_to :step
  14. belongs_to :item
  15. end
  16. class NestedItem
  17. belongs_to :parent_item, foreign_key: :parent_item_id
  18. belongs_to :item
  19. end
  20. 在构建嵌套的 `Items` 时,如何访问关联模型呢?
  21. 例如,对于与 `Step` 相关的 `Item`,可以像这样检索 `StepItem`
  22. ```ruby
  23. step.items.build.step_item # => StepItem

那么在构建项目时,如何访问特定的 NestedItem 模型呢?

  1. item = step.items.build.items.build # => Item
  2. item.??? # 如何检索连接 `item` 与其父级的 `NestedItem`?
  1. 请注意,这里的 "???" 表示您需要填入相应的代码来检索 `NestedItem` 模型。
  2. <details>
  3. <summary>英文:</summary>
  4. Given the following structure where a `Step` has many `Items` and an `Item` has many nested `Items`:
  5. class Step
  6. has_many :step_items
  7. has_many :items, through: :step_items
  8. end
  9. class Item
  10. has_one :step_item
  11. has_one :step, through: :step_item
  12. has_many :nested_items, foreign_key: :parent_item_id
  13. has_many :items, through: :nested_items
  14. end
  15. class StepItem
  16. belongs_to :step
  17. belongs_to :item
  18. end
  19. class NestedItem
  20. belongs_to :parent_item, foreign_key: :parent_item_id
  21. belongs_to :item
  22. end
  23. How could I access the join model when &quot;building&quot; nested `Items`?
  24. For example, for an `Item` that was related to a `Step` I could retrieve the `StepItem` like this:

step.items.build.step_item # => StepItem

  1. How would I access the specific instance of the `NestedItem` model when building items:

item = step.items.build.items.build # => Item
item.??? # How do I retrieve the NestedItem that joins item to it's parent?

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. Your terminology is a little off, so I'm not quite sure what you're asking:
  5. 例如,对于与步骤相关的项目,我可以这样检索StepItem:
  6. step.items.build.step_item # => StepItem
  7. 这不是检索项目,而是创建一个新项目。但我认为你想要的是:
  8. parent_item = step.items.build
  9. item = parent_item.items.build # => Item
  10. parent_item.nested_items.find{|ne| ne.item == item}
  11. 但你可能真正想要的是从项目到parent_item
    展开收缩
    的关系。
  12. 真正的问题可能是为什么你想要访问nested_item,因为它已经完成了它的整个工作。
  13. <details>
  14. <summary>英文:</summary>
  15. Your terminology is a little off, so I&#39;m not quite sure what you&#39;re asking:

For example, for an Item that was related to a Step I could retrieve the StepItem like this:

step.items.build.step_item # => StepItem

  1. That&#39;s not retrieving an item, that&#39;s building a new one. But I *think* what you want is

parent_item = step.items.build
item = parent_item.items.build # => Item
parent_item.nested_items.find{|ne| ne.item == item}

  1. But you may really want a relationship from Item to parent_item
    展开收缩
    .
  2. The real question may be why you want to get at the nested_item at all since it has done its whole job already.
  3. </details>

huangapple
  • 本文由 发表于 2023年5月22日 05:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定