我试图实现包括在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负载Book
和LineItem
和显示的记录Book
,没有记录LineItem
.我尝试时得到未定义的方法错误@author.books.line_items
.也没有@author.line_items
工作.
请问如何获取LineItem
记录Author
?谢谢!
我正在使用 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)