# How to get recently saved record

**URL:** <https://www.ruby-forum.com/t/how-to-get-recently-saved-record/108137>\
**Category:** Rails\
**Created:** [June 29, 2007, 11:14am UTC](https://www.ruby-forum.com/t/how-to-get-recently-saved-record/108137 "2007-06-29T11:14:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 29, 2007, 11:14am UTC](https://www.ruby-forum.com/t/how-to-get-recently-saved-record/108137/1 "2007-06-29T11:14:56Z")

</div>

hi all…

im new to rails…im developin a app in which wen a record is created  
through create method in controller class…my create method in  
controller is shown below…

voicechats—\>table1  
chatusers----\>table2 foreign key of table2 refers table1’s primary  
key…

def create  
@chatuser = Chatuser.new(params[:chatuser])

if Voicechat.find(@chatuser.voicechat\_id).password ==  
@chatuser.password

if @chatuser.save

flash[:notice] = ‘Sign in Successfull…’

@chatuser\_pages, @chatusers  
=paginate(:chatusers, :conditions=\>[“voicechat\_id=?”,  
@chatuser.voicechat\_id],:per\_page=\>18)

redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
else  
flash[:notice]=‘Plz enter correct password.’  
render :action =\> ‘new’

end  
end

and here comes my list method…

def list

@chatuser\_pages, @chatusers  
=paginate(:chatusers, :conditions=\>[“voicechat\_id=?”,  
@chatuser.voicechat\_id],:per\_page=\>18)

end

the problem is that @chatuser is local to create method… but i want  
the foreign key of that record…so that i can use it in list  
method…is there a way to get the recently saved record…so that i  
can use that…plz anyone help me out…it would be great even if u  
provide alternate solution…thanks in advance…

---

<div class="post-metadata">

**Author:** ![mani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mani](https://www.ruby-forum.com/u/mani)\
**Post date:** [June 29, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-get-recently-saved-record/108137/2 "2007-06-29T17:28:45Z")

</div>

The create method I assume is from a user request, so it is a  
different HTTP request from the list. The data from the create  
method is long gone, and could even be in a different mongrel server.

If you want to highlight the last created record you will need to  
store in each record the creation time and the creating user, then  
compare those to the current user and highlight the newest record in  
the list. There are standard column names for time stamps that will  
be set by AR when a record is saved, but setting the user will need  
to be done in a before\_save filter or in your app logic.

Michael
