Last object edited appearing in form on new object page

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

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

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.

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.

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.

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

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

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.