I have a object User and object Post. A User has_many Posts and a Post belongs_to a User. I can access the posts a user has using syntax like:
posts = @user.posts
However, I want to generate an array of ids of the posts a user owns, without looping through each one. Ideally, I'd like to do this:
ids = @user.posts.id
However, I get an error, saying that id is an invalid method for ActiveRecord--etc etc...
Is there any way to neatly do this without having to do a loop like:
ids = []
posts = @user.posts
posts.each |post| do
ids << post.id
end