开发者

Ruby on Rails - Active Record Query - Return items that only have an image

开发者 https://www.devze.com 2023-03-27 16:37 出处:网络
I\'m redeveloping a site that has an image slider on the home page that shows 5 of the latest news stories, however, I only want it to show the latest 5 stories that have at least one image in the ima

I'm redeveloping a site that has an image slider on the home page that shows 5 of the latest news stories, however, I only want it to show the latest 5 stories that have at least one image in the images table.

So my question is, how to I modify the below to return five records with 开发者_如何学Goimages:

@slider_stories = Story.published.all(:limit => 50, :include => [:images])


Something like that

@slider_stories = Story.published.all(:limit => 50, :include => [:images], :conditions => "DISTINCT(stories.id), stories.*, images.*")

or

@slider_stories_ids = Image.all(:select => "DISTINCT(story_id)").map(&:story_id)
@slider_stories = Story.published.all(:limit => 50, :include => [:images], :conditions => {:id => @lider_stories_ids})
0

精彩评论

暂无评论...
验证码 换一张
取 消