Re: Strange Problem With Unwanted, Transient Caching

Anthony Carlos
Wed, 07 Mar 2007 14:40:45 -0000

Andy:

Yeah, that''s weird. I haven''t seen anything like it. However,
I just
want to confirm if you made a typo in your code:

    def edit
      puts "params: #{params.inspect}"
      puts "params[:id].nil?: #{params[:id].nil?}"

      o = Order.find_by_id(id) # SHOULD THIS BE params[:id] ???

      puts "order: #{o.nil? ? ''nil'' : o.inspect}"
      o = Order.find_by_id(id)
      puts "order: #{o.nil? ? ''nil'' : o.inspect}"

      ...
    end

If id really is nil, then I''m stumped.

-Anthony

On Mar 7, 2007, at 8:27 AM, Andrew Stewart wrote:

>
> Hello,
>
> I hope somebody can explain to me what''s going on here because
I''m
> baffled!
>
> In a controller''s action I want to create a new order for a
> customer.  Because I post back to the same action (not RESTful I
> know, but that''s for another day) I use code like this:
>
>    def edit
>      @order = Order.find_by_id(params[:id]) || Order.new
>      ...
>    end
>
> I noticed that when params[:id] is nil, the finder still retrieves an
> order -- the order whose id matches the :customer_id which is in my
> params hash.  Weird.
>
> I then noticed that if I ran the finder a second time, it correctly
> returned nil.  Weirder.
>
> And I found the same behaviour if I replaced the find_by_id with
> find_by_sql using the SQL statement it generated in the log.  A weird
> clue?
>
> Putting all this another way, this code generates the output below:
>
>    def edit
>      puts "params: #{params.inspect}"
>      puts "params[:id].nil?: #{params[:id].nil?}"
>      o = Order.find_by_id(id)
>      puts "order: #{o.nil? ? ''nil'' :
o.inspect}"
>      o = Order.find_by_id(id)
>      puts "order: #{o.nil? ? ''nil'' :
o.inspect}"
>
>      ...
>    end
>
> This prints out:
>
>    params: {"action"=>"edit",
"controller"=>"orders",
> "customer_id"=>"1375"}
>    params[:id].nil?: true
>    order: #<Order:0x35f9a5c
@attributes={"id"=>"1375",
> "customer_id"=>"502",
"required_at"=>nil, "created_at"=>"2004-01-22
> 12:00:00"}>
>    order: nil
>
> So my questions are:
>
> - Why does this finder/query pull out an order the first time instead
> of nil?
> - Why is the order it wrongly pulls out the one whose id matches the
> customer_id?
> - Why does the query magically work the second time?
> - Have I lost the plot? :)
>
> The fact that find_by_sql exhibits the same weird behaviour leads me
> to suspect the database (MySQL).  But if that were caching, surely it
> would return the same result both times?
>
> Thanks and regards,
> Andy Stewart