Forum: Ruby on Rails def list - paginate question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F779209a7f02c754eda7b505898347f9?d=identicon&s=25 Rob Balfour (robtotheb)
on 2006-04-27 18:53
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-27 19:17
(Received via mailing list)
@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 Balfour wrote:
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
F779209a7f02c754eda7b505898347f9?d=identicon&s=25 Rob Balfour (robtotheb)
on 2006-04-27 19:35
Kevin Olbrich 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 Balfour wrote:
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> _Kevin

Didn't work
I dont have a user_id attribute in the model
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2006-04-27 19:45
(Received via mailing list)
On Apr 27, 2006, at 9:53 AM, Rob Balfour 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 Russell
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-27 20:08
(Received via mailing list)
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 Balfour wrote:
>>>http://lists.rubyonrails.org/mailman/listinfo/rails
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
F779209a7f02c754eda7b505898347f9?d=identicon&s=25 Rob Balfour (robtotheb)
on 2006-04-28 12:32
Kevin Olbrich 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 Balfour wrote:
>>>>http://lists.rubyonrails.org/mailman/listinfo/rails
>>Posted via http://www.ruby-forum.com/.
>>_______________________________________________
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>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.
This topic is locked and can not be replied to.