# Postback issue

**URL:** <https://www.ruby-forum.com/t/postback-issue/69290>\
**Category:** Rails\
**Created:** [September 20, 2006, 12:37am UTC](https://www.ruby-forum.com/t/postback-issue/69290 "2006-09-20T00:37:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 12:37am UTC](https://www.ruby-forum.com/t/postback-issue/69290/1 "2006-09-20T00:37:21Z")

</div>

Hello, I am attempting to implement the “Clean Up Controllers with  
Postback Actions” Rails Recipe and I am having a problem with it.

# First, the code:

# == Controller Method ==

# def add\_product\_type puts “Params: ‘#{params[:id]}’” puts “PT: #{@product\_type.id}” unless @product\_type.nil? @product\_type = ProductType.find\_by\_id(params[:id]) || ProductType.new puts “PRODUCT TYPE: ‘#{@product\_type.name}’ - ‘#{@product\_type.id}’” if request.post? @product\_type.attributes = params[:product\_type] begin if @product\_type.save! @product\_type = nil redirect\_to :action =\> :index and return end @product\_type = nil rescue ActiveRecord::RecordInvalid =\> e end end end

# == View ==

\<%= start\_form\_tag :action =\> ‘add\_product\_type’ %\>

Name: \<%= text\_field 'product\_type', 'name' %\>

 \<% if @product\_type.id.nil? %\> \<%= submit\_tag "Add" %\> \<% else %\> \<%= submit\_tag "Edit" %\> \<% end %\>
# \<%= end\_form\_tag %\>

# So, I can submit a new type and it works flawlessly. If I immediately return to the add a product page, it fills in the last product type’s name. The output from the above puts’s:

# Params: ‘’ PRODUCT TYPE: ‘’ - ‘’ 127.0.0.1 - - [19/Sep/2006:18:22:39 EDT] “POST /admin/add\_product\_type HTTP/1.1” 302 93 [http://localhost:3000/admin/add\_product\_type](http://localhost:3000/admin/add_product_type) → /admin/add\_product\_type 127.0.0.1 - - [19/Sep/2006:18:22:39 EDT] “GET /admin HTTP/1.1” 200 4569 [http://localhost:3000/admin/add\_product\_type](http://localhost:3000/admin/add_product_type) → /admin Params: ‘’ PRODUCT TYPE: ‘gg’ - ‘16’ \<--------------------------------------------- ??? 127.0.0.1 - - [19/Sep/2006:18:22:42 EDT] “GET /admin/add\_product\_type HTTP/1.1” 200 1053 [http://localhost:3000/admin](http://localhost:3000/admin) → /admin/add\_product\_type

The params are nil and yet it is finding a product type? Am I doing  
something incorrectly here?

I am running the latest rails out of trunk as of today and have the  
issue when using webrick, lighttpd and mongrel.

Thanks in advance for any help. I’m not certain where to begin with  
this one.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/2 "2006-09-20T15:30:47Z")

</div>

I tested it this morning in ./script/console:

> > pt = ProductType.find( :first )  
> > =\> #\<ProductType:0x253b410 @attributes={“name”=\>“Poster”, “id”=\>“1”}\>
> 
> > ProductType.find\_by\_id( nil )  
> > =\> nil
> 
> > p = ProductType.new  
> > =\> #\<ProductType:0x2535150 @attributes={“name”=\>nil}, @new\_record=true\>
> 
> > p.name = “Yahar”  
> > =\> “Yahar”
> 
> > p.save!  
> > =\> true
> 
> > ProductType.find\_by\_id( nil )  
> > =\> #\<ProductType:0x25230f4 @attributes={“name”=\>“Yahar”, “id”=\>“5”}\>

So evidently if you find\_by\_id( nil ), it returns the last object  
created? That sort of breaks the recipe in that it populates the form  
when you immediately return to the page.

Essentially, I take it that I have to explicitly check for the  
existence of params[:id] which muddies the code a bit.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/3 "2006-09-20T18:41:12Z")

</div>

Hi,

On Wed, 2006-20-09 at 06:22 -0700, removed\_email\_address@domain.invalid wrote:

> I tested it this morning in ./script/console:
> 
> So evidently if you find\_by\_id( nil ), it returns the last object  
> created? That sort of breaks the recipe in that it populates the form  
> when you immediately return to the page.
> 
> Essentially, I take it that I have to explicitly check for the  
> existence of params[:id] which muddies the code a bit.

I’m guessing that you’re using MySQL? Try this in MySQL itself:

```
    CREATE TABLE foo (id INT(11) NOT NULL AUTO_INCREMENT,
                             name VARCHAR(255),
                             PRIMARY KEY(id));

    INSERT INTO foo (name) VALUES("Tada");
    SELECT * FROM foo WHERE id IS NULL;
    SELECT * FROM foo WHERE id IS NULL;

```

The first SELECT will return the record you just inserted. The second  
select returns the empty set, as should be expected.

Seems to be a problem on tables that have an AUTO\_INCREMENT field. It  
doesn’t seem to matter whether the table is MyISAM or InnoDB. I  
recently got bit by this when two users created their accounts nearly  
simultaneously and the second one got the first ones profile.

Not nice.

This does not happen in Postgres, btw.

Rick T.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/4 "2006-09-20T19:11:18Z")

</div>

Wow. I suppose I can test for empty params[:id] first, but that both  
makes the code a bit longer and more importantly means I am writing  
code to compensate for a specific database implementation.

Did you solve the problem similarly?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/5 "2006-09-20T19:25:20Z")

</div>

Hi,

On Wed, 2006-20-09 at 12:44 -0400, Michael C. wrote:

> Transactions don’t help this?

Nope, transactions do not seem to help either. It’s not a good thing  
since the identical query twice in a row, with no other changes to the  
DB, yield different results.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/6 "2006-09-20T18:59:14Z")

</div>

Transactions don’t help this?

On 9/20/06, Rick T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> ```
> SELECT * FROM foo WHERE id IS NULL;
> 
> ```
> 
> Not nice.
> 
> This does not happen in Postgres, btw.
> 
> Rick T.
> 
> >

–  
No matter how far down the wrong road you’ve gone, turn back. – Turkish  
proverb

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/7 "2006-09-20T19:31:21Z")

</div>

Is this expected behavior (a ‘feature’)?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/8 "2006-09-20T19:39:23Z")

</div>

Hi,

On Wed, 2006-20-09 at 10:00 -0700, removed\_email\_address@domain.invalid wrote:

> Wow. I suppose I can test for empty params[:id] first, but that both  
> makes the code a bit longer and more importantly means I am writing  
> code to compensate for a specific database implementation.
> 
> Did you solve the problem similarly?

In the case where I first ran across the issue, yes. In new projects,  
I’m just using Postgres instead.

Rick

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 20, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/postback-issue/69290/9 "2006-09-20T20:01:25Z")

</div>

Hmm, I have used postgres in the past but I recall its clustering  
capabilities at the time seemed rather limiting. I’ll have a look  
again.

Anyway, thanks a lot for clarifying this for me. I really appreciate  
it.

Best regards.
