加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails中更优雅的方式

发布时间:2020-12-17 02:37:09 所属栏目:百科 来源:网络整理
导读:我有以下代码: @countries = Country.find( :all,:order = 'name' )@countries_with_tips = []@countries.each do |country| if country.tips.any? @countries_with_tips.push( country ) endend 我得到每个至少有一个小费的国家.一个国家有一个很多提示,一
我有以下代码:

@countries = Country.find( :all,:order => 'name' )

@countries_with_tips = []

@countries.each do |country|
  if country.tips.any?
    @countries_with_tips.push( country )
  end
end

我得到每个至少有一个小费的国家.一个国家有一个很多提示,一个提示属于一个国家

有用.但对于Ruby来说,它似乎有点不雅.有没有更好的办法?

提前致谢

理查德

解决方法

@countries_with_tips = Country.joins(:tips).order(:name).uniq

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读