Working with user data

I am a Rails newbie and I am trying to build a test site in which users
will have their own sets of data.

I understand how to manipulate shared data very well. Most of the Rails
examples out there seem to deal with such shared data. I am struggling
with the “best practice” method for a user to work with their own data
however (and not be able to view/edit other users’ data).

I am using LoginEngine and have the authentication working correctly.

I started by generating scaffolding for a model named “listing”. Each
user will have their own listings. The Listing model belongs_to user and
of course User has_many listings.

The scaffolding for ListingsController made this:

def list
@listing_pages, @listings = paginate :listings, :per_page => 10
end

Simple enough, but this shows all listings from any user of course. I’m
able to get it to work as I want by changing it to this:

def list
@listings = current_user.listings
@listing_pages = Paginator.new self, @listings.count, 10,
params[:page]
end

Is this the right way to restrict the data to the logged-in user (with
pagination)? It seems like I will be repeating myself quite often for
all the various user data. I have a feeling there is a simpler way. I
suppose I can reference current_user.listings directly in the View but
that does not give me pagination.

I tried to do pagination based on the scaffolding like this but it did
not work:

def list
@listing_pages, @listings = paginate :current_user.listings,
:per_page => 10
end

I thought this seemed logical but perhaps I am missing something
important about pagination or the Rails structure in general.

Thanks for any help or pointers to docs about this sort of thing!

Carl

i dont know how to do it exactly, but imho there is a way to set a scope
on
the AR model. when you set the scope to user_id = session[:user] (or sth
else) in eg a before_filter of that controller, you’ll only get listings
which have user_id = session[:user], even when doing Listing.find(:all)
this way you can use the rails scaffold controller, add 1 line of code
and
get what you want. isnt that great?
for more info on that, search the manual for AR

2006/8/15, Carl J. [email protected]:

Simple enough, but this shows all listings from any user of course. I’m
all the various user data. I have a feeling there is a simpler way. I

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Michael S. [email protected]

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium

I found my answer here:

http://rails.techno-weenie.net/question/2006/6/26/pagination-and-habtm-relationship

Looks like scope is not the right way to do this sort of thing when you
can leverage the model relationship, i.e. current_user.listings.

The ‘pagination’ function in that above web page is exactly what I need.
It seems strange that the built-in pagination will only accept a single
class name however. Paginate doesn’t work “out of the box” with model
relationships. Can anyone tell me if there are plans to change this?

Carl

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs