ruby-on-rails - 在 Map 函数中的符号内传递方法
问题描述
有人可以向我解释,我如何在给我 Map 的符号内传递一个方法。这是我的方法
def visited_venues
Profile.find(1432).bookings.map(&:venue).uniq
end
现在我想做类似的事情
def visited_venues
Profile.find(1432).bookings.map(&:venue.kept).uniq
end
这会在我的数据库中返回我所有未丢弃的内容,当我尝试这样做时,总是返回我undefined
,因为地点是一个符号。感谢您的任何帮助和解释。
解决方案
而不是做
Profile.find(1432).bookings.map(&:venue.kept).uniq
尝试
Profile.find(1432).bookings.map { |booking| booking.venue.kept }.uniq
在 Pry 中进行测试
["hi", "bye"].map(&:upcase) -> ["HI", "BYE"]
["hi", "bye"].map(&:upcase.downcase) -> ["HI", "BYE"]
请记住
(&:method) is just a shortcut for { |el| el.method }
推荐阅读
- matlab - (Matlab) SVM 优化器因大矩阵而失败
- aframe - 为什么在 1.0.3 版本的 Google Chrome 中,光标在 Android 上的 VR 模式下不起作用?
- java - Vaadin 14 Grid + Dialog:关闭对话框后如何刷新网格?
- c# - Unity 奖励视频广告注册多次。统一广告
- javascript - React - mapStateToProps - “状态未定义”
- sql - WORDPRESS 数据库如何从数据库返回元值?
- java - Selenium Actions dragAndDrop unexpectedly navigates to google search page
- php - Use PHP to move files in Windows CMD
- json - 如何从powershell脚本中的json文件中选择/斜线之后的单词
- scikit-learn - Stop sign flips in sklearn's PCA