How to get the list of users' id

Hi,

I have a question that how to take a list of user on user table

def show_pic

@id = User.find(:all, :select => ‘users.id’)
@id.each do |i|
@pic = ActiveRecord::Base.connection.select_value(
select picture
from albums a
inner join Users u
on u.id = a.id
where u.id = i )
end
end

please give me some advices…

THanls you so much

On 13 June 2011 18:33, Yennie [email protected] wrote:

please give me some advices…

Assuming your users have a “has_many :pictures” relationship:

@pictures = User.all.map(&:pictures).flatten

Or if your Users have many Albums, and Albums have many Pictures:

@pictures = User.all.map(&:albums).flatten.map(&:pictures).flatten

(can probably use .inject to make that smaller, but you can look that
up in the api docs)

then you can iterate @pictures however you want in your views.

but… hang on…

Aren’t you just getting a list of all the Pictures? Why not use
“Picture.all”?

Sorry, I mean get the list of user id …
cuz my query depends on user.id … so i want to have a list which is
array
to go through
the album…

I don’t see what you are trying to achieve here. Surely you just have a
list of Users:

User.all

Given Users has_many :albums, you could handle this in the view along
with pictures. To limit the number of requests to the database, make
sure to include the associations:

User.all.include :albums

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs