List Info

Thread: Working with user data




Working with user data
user name
2006-08-15 04:53:39
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

-- 
Posted via http://www.ruby-forum.com
/.
_______________________________________________
Rails mailing list
Railslists.rubyonrails.org
h
ttp://lists.rubyonrails.org/mailman/listinfo/rails
Working with user data
user name
2006-08-15 06:49:55
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 Johnson <yahoo.com">carl_ivaryahoo.com>:
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&quot; 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:

&nbsp; def list
 ; &nbsp; listing_pages, listings = paginate :listings, :per_page => 10
 &nbsp;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:

&nbsp; def list
 ; &nbsp; listings = current_user.listings
 &nbsp; &nbsp;listing_pages = Paginator.new self, listings.count, 10,
params[:page]
&nbsp; 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:

&nbsp; def list
 ; &nbsp; 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

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
lists.rubyonrails.org"> Railslists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails



--
Michael Siebert < siebert-wd.de">infosiebert-wd.de>

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium
Working with user data
user name
2006-08-15 20:58:43
I found my answer here:

http://rails.techno-weenie.ne
t/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

-- 
Posted via http://www.ruby-forum.com
/.
_______________________________________________
Rails mailing list
Railslists.rubyonrails.org
h
ttp://lists.rubyonrails.org/mailman/listinfo/rails
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )