ruby-on-rails - array iteration for ruby on rails
问题描述
Each favorite consists of the customer id and restaurant id. I am basically trying to iterate through all the favorites and I am trying to see if the current customer.id is equal to the customer_id of the instance and then return it. Im not sure if select was the right iterator I should have used... I am confused as to the next steps I should take. In the end I will have to find a way to get the names of the restaurant which I have an idea how to do but for now I need to find a way just to get the instances of the favorites first that belong to the specific customer I am working with and have them saved into a variable so I can then later on do another iteration through them to get the names of the restaurants. I used a binding.pry but it seems like the customer_favs is coming out to be nil...
def view_all_favorites(customer)
customer_favs = Favorite.all.select do |cus_favs|
cus_favs.customer_id == customer.id
binding.pry
end
end
解决方案
It appears a Customer
has_many
Favorite
's right? In that case, ActiveRecord gives you this for free via customer.favorites
推荐阅读
- xml-parsing - Java 中的 Sax Stax 块处理
- oracle - PowerBI 与 Oracle-DB 连接:ORA-12154
- python - 字母之间具有恒定字母距离的正则表达式
- html - 调用控制器方法而不加载空白视图
- google-apps-script - 如何将项目执行删除到 Google 脚本中
- android - 迁移到 Gradle 插件 3.2.1
- javascript - 如何在反应路由器中的每次路由更改时监听窗口负载?
- javascript - 如何在 React 中使用 audioWorklet.addModule?
- python - 沿任意轴的 numpy 广播加法
- postgresql - 如何在 Go 中正确绑定 postgresql 变量?