小编Mat*_*ano的帖子

Rails 5.1:检索与包含嵌套关联的记录

我试图实现包括has_many/ belongs_to类似于此示例关联:

class Author < ApplicationRecord
  has_many :books, -> { includes :line_items }
end

class Book < ApplicationRecord
  belongs_to :author
  has_many :line_items
end

class LineItem < ApplicationRecord
  belongs_to :book
end
Run Code Online (Sandbox Code Playgroud)

在那一刻,我做@author.books我看到我的控制台IT负载BookLineItem和显示的记录Book,没有记录LineItem.我尝试时得到未定义的方法错误@author.books.line_items.也没有@author.line_items工作.

请问如何获取LineItem记录Author?谢谢!

ruby activerecord ruby-on-rails ruby-on-rails-5.1

3
推荐指数
1
解决办法
344
查看次数

在 Rails 中,如何删除数组中的所有对象?

我正在使用 Rails 5,我想删除一组对象。在之前的帖子中,我读到“destroy_all”是真理和光明。我有两个对象数组,我将它们减去以获得第三个数组

  unused_currencies = all_currencies - currencies_from_feed
  unused_currencies.destroy_all
Run Code Online (Sandbox Code Playgroud)

但是在使用时出现destroy_all了这个错误:

NoMethodError: undefined method `destroy_all' for #<Array:0x007feea8878770>
Run Code Online (Sandbox Code Playgroud)

ruby arrays ruby-on-rails destroy ruby-on-rails-5

0
推荐指数
1
解决办法
4960
查看次数