Def list - paginate question


#1

How do I adapt this code to only list articles made by the user logged
into the session?

def list
@article_pages, @article = paginate :articles, :per_page => 10
end


#2

@article_pages, @article = paginate :articles, :per_page => 10,
:condition=>[‘user_id = ?’, current_user.id]

Assuming there is a user_id attribute for the model

On Thursday, April 27, 2006, at 6:53 PM, Rob B. wrote:

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#3

Kevin O. wrote:

@article_pages, @article = paginate :articles, :per_page => 10,
:condition=>[‘user_id = ?’, current_user.id]

Assuming there is a user_id attribute for the model

On Thursday, April 27, 2006, at 6:53 PM, Rob B. wrote:

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin

Didn’t work
I dont have a user_id attribute in the model


#4

On Apr 27, 2006, at 9:53 AM, Rob B. wrote:

How do I adapt this code to only list articles made by the user logged
into the session?

def list
@article_pages, @article = paginate :articles, :per_page => 10
end

The paginate method accepts the :conditions option.

http://api.rubyonrails.org/classes/ActionController/
Pagination.html#M000104

Here’s an example with a named placeholder…

values = { :user_id => session[:user_id] }
paginate :articles, :conditions => [ ‘user_id = :user_id’,
values ], :per_page => 10

Hope that helps!

Cheers,

Robby

Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting


www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]


#5

You will just have to adjust the :conditions clause to match your
particular user model and method for tracking the current_user, but
otherwise it will work.

Your article model really needs to have a ‘belongs_to :user’ association
so that you can tell which articles belong to which user. If you don’t
have that, or something equivalent, then there is no way to filter the
articles properly.

on Thursday, April 27, 2006, at 7:35 PM, Rob B. wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails
Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin


#6

Kevin O. wrote:

You will just have to adjust the :conditions clause to match your
particular user model and method for tracking the current_user, but
otherwise it will work.

Your article model really needs to have a ‘belongs_to :user’ association
so that you can tell which articles belong to which user. If you don’t
have that, or something equivalent, then there is no way to filter the
articles properly.

on Thursday, April 27, 2006, at 7:35 PM, Rob B. wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails
Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin

I do have a belongs to :user in my article model. Im just not sure how
to adjust the :conditions clause in the controller. I used salted login
gen to create the user model so not sure how its tracking the
current_user.