I use this code to select all the record with name = 'g' @resulttag = Tag.find(:all, :conditions => "name = 'g'")
Then I want to select all related Entries record, by this code @r = @resulttag.entries
But it is not work, @r still storing the same data with @resulttag
(The array storing Tag records, not Entry records)
It's not working because @resulttag is an array of tags, not a single
tag
@resulttag.entries.collect(&:entries) will give you an array
containing an array for each tag, or if you want an array with all
entries,
@resulttag.entries.collect(&:entries).flatten will do the job
You could also approach it the other way round:
Entry.find_by_sql <<_SQL SELECT * from entries INNER JOIN entries_tags on entries.id = entry_id INNER JOIN tags on tags.id = tag_id where name = 'g' _SQL
Fred