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 -- Posted via http://www.ruby-forum.com/.
@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:>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 > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
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 > > > _KevinDidn''t work I dont have a user_id attribute in the model -- Posted via http://www.ruby-forum.com/.
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]
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:>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 > > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
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 > > > _KevinI 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. -- Posted via http://www.ruby-forum.com/.