# Last object edited appearing in form on new object page

**URL:** <https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295>\
**Category:** Rails\
**Created:** [November 4, 2006, 1:06am UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295 "2006-11-04T01:06:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 4, 2006, 1:06am UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/1 "2006-11-04T01:06:23Z")

</div>

So my last two projects i keep getting this wierd thing…

The last opbejct I created repopulates my edit form when i go to create  
a new object WITHOUT THE ID BEINNG PASSED.  
Where is it getting this id from?

I have a edit function in my controller ala rails recipies ‘cleaning up  
your controller’ page:

def edit  
#for a parent id  
@sport = Sport.find(params[:sport\_id])

#for a select list  
@field\_types = FieldType.find(:all)

@sport\_field = SportField.find\_by\_id(params[:id]) || SportField.new

#log output below  
logger.error(@sport\_field.id)

if request.post?  
@sport\_field.attributes = params[:sport\_field]  
@sport.sport\_fields \<\< @sport\_field  
redirect\_to :action =\> “list”, :sport\_id =\> params[:sport\_id] and  
return if @sport\_field.save  
end  
end

So if i hit my list page and then click the new link (\<%= link\_to “New  
field for: #{@sport.name}”, :action =\> ‘edit’, :sport\_id =\> @sport.id  
%\>)  
everything is cool and my new object is created and saved, I am  
redirected back to the list page

Now if I hit new again it somehow finds the last object i created and  
popuates the field values AND the form action with the id of the  
previous object.

## This is the wierdest thing… the log file:

Processing SportFieldController#edit (for 127.0.0.1 at 2006-11-03  
18:57:09) [GET]  
Session ID: 5d3f3c4e8af29b3ad4dc5da66034a1cd  
Parameters: {“action”=\>“edit”, “sport\_id”=\>“9”,  
“controller”=\>“admin/sport\_field”}  
Sport Columns (0.008294) SHOW FIELDS FROM sports  
Sport Load (0.006463) SELECT \* FROM sports WHERE (sports.id = 9)  
FieldType Load (0.000939) SELECT \* FROM field\_types  
SportField Columns (0.007362) SHOW FIELDS FROM sport\_fields  
SportField Load (0.007752) SELECT \* FROM sport\_fields WHERE  
(sport\_fields.`id` IS NULL ) LIMIT 1  
41  
Rendering within layouts/main  
Rendering admin/sport\_field/edit

* * *

So it selects the opject with ID NULL but finds and logs a id of 40 (in  
this case)

I have switched to database session mgmt just in case. No change.  
Anybody seen this before?  
Thanks,  
-Dinshaw

---

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 4, 2006, 8:59am UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/2 "2006-11-04T08:59:03Z")

</div>

Dinshaw G. wrote:

> ## This is the wierdest thing… the log file:
> 
> Processing SportFieldController#edit (for 127.0.0.1 at 2006-11-03  
> 18:57:09) [GET]  
> Session ID: 5d3f3c4e8af29b3ad4dc5da66034a1cd  
> Parameters: {“action”=\>“edit”, “sport\_id”=\>“9”,  
> “controller”=\>“admin/sport\_field”}  
> Sport Columns (0.008294) SHOW FIELDS FROM sports  
> Sport Load (0.006463) SELECT \* FROM sports WHERE (sports.id = 9)  
> FieldType Load (0.000939) SELECT \* FROM field\_types  
> SportField Columns (0.007362) SHOW FIELDS FROM sport\_fields  
> SportField Load (0.007752) SELECT \* FROM sport\_fields WHERE  
> (sport\_fields.`id` IS NULL ) LIMIT 1  
> 41  
> Rendering within layouts/main  
> Rendering admin/sport\_field/edit
> 
> * * *
> 
> So it selects the opject with ID NULL but finds and logs a id of 40 (in  
> this case)

Sounds bizarre.

There is a plugin called query trace:  
[http://www.agilewebdevelopment.com/plugins/querytrace](http://www.agilewebdevelopment.com/plugins/querytrace)

It places a little call stack in you log file by each sql statement.  
This should tell you where these queries are getting called from at  
least.

---

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/3 "2006-11-07T11:00:09Z")

</div>

Dinshaw G. wrote:

> this case)  
> Which database adapter and Rails version are you using?

In any case it’d be better to switch to:

@sport\_field = params[:id] ? SportField.find(params[:id]) :  
SportField.new

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 7, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/4 "2006-11-07T16:18:38Z")

</div>

Hi and thanks for the reply.  
I am using mysql and i checked out the head of the repository into  
vendor at the begining of this month.

Here is something else strange, if you refreash the page that has the  
phantom object’s info, it goes away and acts as if (correctly) I was  
createing a new object.

---

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 7, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/5 "2006-11-07T18:44:13Z")

</div>

Mark Reginald J. wrote:

> So change your code in the way suggested in this post, which is  
> the same as I suggested in my original reply.

Thank you very much Mark. I was heistant to change that because it is  
straight out ot Rails Recipies. I will plug in your fix as soon as I am  
off the clock.  
Thanks again  
-Dinshaw

---

<div class="post-metadata">

**Author:** ![dinshaw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/edb3f5/32.png) [@dinshaw](https://www.ruby-forum.com/u/dinshaw)\
**Post date:** [November 7, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/last-object-edited-appearing-in-form-on-new-object-page/74295/6 "2006-11-07T18:32:54Z")

</div>

Dinshaw G. wrote:

> Hi and thanks for the reply.  
> I am using mysql and i checked out the head of the repository into  
> vendor at the begining of this month.
> 
> Here is something else strange, if you refreash the page that has the  
> phantom object’s info, it goes away and acts as if (correctly) I was  
> createing a new object.

The reason for what you’re seeing was just described in another thread:  
[Find\_by\_id vs. find in postback action - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/70940#164187)

So change your code in the way suggested in this post, which is  
the same as I suggested in my original reply.

–  
We develop, watch us RoR, in numbers too big to ignore.
