How to filter a model based on two parameters

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.

On 8 May 2012 17:09, Christopher D. [email protected] wrote:

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

Colin!

Thanks for the help. I ended up using something similar.

@instvar = Model.where( :user_id => current_user.id, :itatt => 1 )

Again, I really appreciate the feedback

On 9 May 2012 03:38, Christopher D. [email protected] wrote:

Colin!

Thanks for the help. I ended up using something similar.

@instvar = Model.where( :user_id => current_user.id, :itatt => 1 )

What is wrong with using current_user.posts?

Colin