I have the following code:
@countries = Country.find( :all, :order => 'name' )
@countries_with_tips = []
@countries.each do |country|
if country.tips.any?
@countries_with_tips.push( country )
end
end
I am getting each country that has at least one tip. A country has_many tips and a tip belongs_to a country
It works. But for Ruby it seems a little unelegant. Is there a better way?
Thanks in advance
Richard
@countries_with_tips = Country.joins(:tips).order(:name).uniq
@countries_with_tips = @countries.select{|country| country.tips.present? }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.