# Showing current user's information and saving

**URL:** <https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424>\
**Category:** Rails\
**Created:** [April 2, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424 "2006-04-02T21:27:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 2, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/1 "2006-04-02T21:27:14Z")

</div>

Hi,

I am logging users in and keeping them in session. The problem I am  
having is showing only a certain logged in user’s information.

In the Adverts\_Controler:

def show  
@advert = Advert.find(params[:id])  
end

I am also having issues with saving an advert for an user:  
def create  
@advert = Advert.new(params[:advert])  
if @advert.save  
flash[:notice] = ‘Advert was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

How do I get the user\_id from the session and use it in above functions  
to only show users’s adverts and to pass user\_id to the create function  
to create an advert for the logged in user?

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 2, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/2 "2006-04-02T21:46:38Z")

</div>

–  
– Tom M.

On Apr 2, 2006, at 12:27 PM, david wrote:

> How do I get the user\_id from the session and use it in above  
> functions  
> to only show users’s adverts and to pass user\_id to the create  
> function  
> to create an advert for the logged in user?

def show  
@user = User.find(session[:user\_id]) # Ideally, put this in  
application.rb and reference via before\_filter  
@advert = @user.adverts.find(params[:id])  
end

def create  
@user = User.find(session[:user\_id]) # Ideally, put this in  
application.rb and reference via before\_filter

```
 @advert = Advert.new(params[:advert])

 @user.adverts << Advert.new(params[:advert])

 if @advert.save
   flash[:notice] = 'Advert was successfully created.'
   redirect_to :action => 'list'
 else
   render :action => 'new'
 end

```

end

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 2:14am UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/3 "2006-04-03T02:14:15Z")

</div>

> def show  
> @user = User.find(session[:user\_id]) # Ideally, put this in  
> application.rb and reference via before\_filter  
> @advert = @user.adverts.find(params[:id])  
> end
> 
> def create  
> @user = User.find(session[:user\_id]) # Ideally, put this in  
> application.rb and reference via before\_filter
> 
> ```
> @advert = Advert.new(params[:advert])
> 
> @user.adverts << Advert.new(params[:advert])
> 
> if @advert.save
> flash[:notice] = 'Advert was successfully created.'
> redirect_to :action => 'list'
> else
> render :action => 'new'
> end
> 
> ```
> 
> end
> 
> –  
> – Tom M.

Thanks for your help Tom.

The create funtion creates double entries in the DB one correctly and  
the other it creates an entry with user\_id 0, which is bad.

The show function still shows all instead of filtering on user\_id.

Any ideas?

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 2:22am UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/4 "2006-04-03T02:22:19Z")

</div>

> > –  
> > – Tom M.
> 
> Thanks for your help Tom.
> 
> The create funtion creates double entries in the DB one correctly and  
> the other it creates an entry with user\_id 0, which is bad.
> 
> The show function still shows all instead of filtering on user\_id.
> 
> Any ideas?

I answered my own question:

@user = User.find(session[:user\_id]) # Ideally, put this  
inapplication.rb and reference via before\_filter  
@advert = Advert.new(params[:advert])  
@user.adverts \<\< @advert

This code works.

If you call new twice, then you are going to get 2 entries in the DB of  
course.

I am still stuggeling with filtering the show function on user\_id  
though.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 3:46am UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/5 "2006-04-03T03:46:07Z")

</div>

david wrote:

> I am still stuggeling with filtering the show function on user\_id  
> though.

This code only finds a single advert.

def show  
@user = User.find(session[:user\_id]) # Ideally, put this in  
application.rb and reference via before\_filter  
@advert = @user.adverts.find(params[:id])  
end

Use this to find all the adverts related to the current user:

def show  
@user = User.find(session[:user\_id]) # Ideally, put this in  
application.rb and reference via before\_filter  
@adverts = @user.adverts  
end

If the associations are correct in the model, you should always be able  
to find a user’s adverts with the “@user.adverts” method.

Hope this helps,  
Jeff

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [April 3, 2006, 7:57am UTC](https://www.ruby-forum.com/t/showing-current-users-information-and-saving/93424/6 "2006-04-03T07:57:57Z")

</div>

On Apr 2, 2006, at 5:22 PM, david wrote:

> > Any ideas?  
> > If you call new twice, then you are going to get 2 entries in the  
> > DB of  
> > course.

Yes, do what I meant, not what I said. 🙂

However, calling new shouldn’t create a row until the object is saved.

–  
– Tom M.
