On 8 May 2012 17:09, Christopher D. <[email protected]> wrote: > Hi I’m looking to query a Post model based on two attributes. > - the current user’s id > - a “1” in an integer attribute that I have created > > I hope to show the title each one of this persons posts that have a 1 in > the integer attribute > > This is what I am thinking of having > > Posts Controller (I need help here, below is what I was thinking) > def selectview > @posts = Post.find( :user_id == session[:user_id], :intatt == 1) > End > > Posts view > <%= @posts.each do |post| %> > <%= post.title %> > <% end %> > > I am most concerned about how to code the controller, any help would be > appreciated.
Assuming that you have set up the relationships properly, so User has_many posts then to get all the posts for the current user you can just say current_user.posts, then if you want intat to be 1 also you just say current_user.posts.where( :itatt => 1 ) which will give you effectively an array of Post objects. If you don't know what user has_many posts means then have a look at the Rails Guide on ActiveRecord associations, though I strongly suggest you work through some Rails tutorials such as railstutorial.org, which is free to use online, before going any further. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

