jack
jack

Reputation: 454

Rails Query limit

I have a query of pictures like this

pictures = Picture.near([latitude, longitude], 6.8).where("created_at >= :time",{:time => time })

and I took out the tags which is associated with this model (every picture has_many :tags) like so

@tags = Tag.find(:all, :conditions => ["picture_id in (?)",pictures.collect(&:id)])

I used a query to pull out an attribute of each tag

@tags.map(&:tagcontent)

What I need to do is limit the results of the tags that come out. so I replaced

@tags = Tag.find(:all, :conditions => ["picture_id in (?)",pictures.collect(&:id)])

with this

numoftags = 6
tags = Tag.limit(numoftags).find(:all, :conditions => ["picture_id in (?)",pictures.collect(&:id)])

but this only yields 4 tags, when I know there is least 6 that can be pulled out. How should I modify this function.

Upvotes: 2

Views: 11698

Answers (1)

crazycrv
crazycrv

Reputation: 2445

In Rails 3.X you can do something like this (using Arel)

numoftags = 6
@tags = Tag.where(["picture_id in (?)",pictures.collect(&:id)]).limit(nooftags)

Upvotes: 2

Related Questions