2

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.

6
  • Please post from your logs the SQL query that your code is generating. Commented Nov 14, 2011 at 19:24
  • @jdl sorry, im not sure where i can find that Commented Nov 14, 2011 at 19:31
  • RAILS_ROOT/log/development.log (or whatever environment you're running this under) Commented Nov 14, 2011 at 19:36
  • @jdl i can't find the query it's generating there Commented Nov 14, 2011 at 19:47
  • What Rails environment are you using? Commented Nov 14, 2011 at 20:05

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

this raised errors elsewhere in my app (which i fixed, but had to leave out your solution), but that was completely my fault for not explaining well enough, but this solved exactly what i asked for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.