tracking_attribute_changes and using default checkbox form in the view

Dear all,

I get a stupid bug and i don't know if there is something wrong in
rails or if it is me doing a stupid thing.

I have a checkbox called (ass_tg) in my form and when i saved my
form , i write in my model:

  before_save :test

  def test
    puts "-----------------------"
    puts ass_tg_change
    puts "-------------------------"
  end

I should have a nil answer but I get in my console :

Dear all,

Maybe i wasn't so clear, excuse me for my english :wink:

My problem is simple ..
I didn't change the value of my checkboxes in my form but the core
function of rails was saying that the values currently change ! and
that's why rails update the values in mysql... and also return a wrong
information if i use the new fontions like : attributes_change,
attribute_changed?...

I don't understand why ?

thank you for your help !
Antoine

Hi,

This was probably because you used the check_box helper, which makes
sure the browser always sends a value.
See the documentation for the check_box helper:

=== Gotcha

The HTML specification says unchecked check boxes are not successful,
and thus web browsers do not send them. Unfortunately this introduces
a gotcha: if an Invoice model has a paid flag, and in the form that
edits a paid invoice the user unchecks its check box, no paid
parameter is sent. So, any mass-assignment idiom like
   @invoice.update_attributes(params[:invoice])
wouldn’t update the flag.
To prevent this the helper generates a hidden field with the same name
as the checkbox after the very check box. So, the client either sends
only the hidden field (representing the check box is unchecked), or
both fields. Since the HTML specification says key/value pairs have to
be sent in the same order they appear in the form and Rails parameters
extraction always gets the first occurrence of any given key, that
works in ordinary forms.

- Eloy

Sorry, I forgot to add the most important part :slight_smile:

What might be happening is that you have a default value of nil (NULL)
which is then changed to an empty string "". Because the browser sends
a value because of the gotcha workaround.

So the easiest solution would be to have a default value for the field.
Which is something you want to have anyways for a boolean field IMO.

- Eloy

You are right, I have a check box helper (by default, it's sending 0
or 1 ):wink: and also my checkbox is represented in my database by an
integer ==> 0 means unchecked , 1 means check.

So my form is sending 0 because my checkbox is unchecked and before
sending, the attribute of the object had the same value 0 (I checked
in the database to be sure)

So I don't understand why rails tell me the state of this attribute
change ? because the attribute is not changing ... 0 ==> 0

Best regards,
Antoine

I checked the test cases (dirty_test.rb), and it contains a test for
this specifically which passes at least on my env.

   def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
     pirate = Pirate.new
     pirate.parrot_id = 0
     pirate.save!
     # other tests
     pirate.parrot_id = '0'
     assert !pirate.changed?
   end

Could you check this as well, and otherwise try to extract a small
piece of code, into a test or sample app, which shows the behaviour?
If you do so, please file a ticket.

- Eloy

This looks like http://rails.lighthouseapp.com/projects/8994/tickets/1530
(where the test Eloy mentions was added).

That patch has not been released yet, which is probably why Antoine is
seeing it.

Ben

[mailto:rubyonrails-core@googlegroups.com] On Behalf Of Eloy Duran

Ah sorry, I shouldn't have assumed edge. My bad.
Thanks Ben

- Eloy

Thank you Ben for your answer :wink:
Do you know when the patch will be available in rails ?

Best regards :wink:

Antoine

It will be available in the next point release we do from the 2.2
branch (2.2.3) hopefully that will come out relatively soon as there's
a few fixes piled up.

It will be available in the next point release we do from the 2.2
branch (2.2.3) hopefully that will come out relatively soon as there's
a few fixes piled up.

Thank you Ben for your answer :wink:
Do you know when the patch will be available in rails ?

Best regards :wink:

Antoine

Ah sorry, I shouldn't have assumed edge. My bad.
Thanks Ben

- Eloy

> This looks likehttp://rails.lighthouseapp.com/projects/8994/tickets/1530
> (where the test Eloy mentions was added).

> That patch has not been released yet, which is probably why Antoine is
> seeing it.

> Ben

> From: rubyonrails-core@googlegroups.com
> [mailto:rubyonrails-core@googlegroups.com] On Behalf Of Eloy Duran
> Sent: 12 January 2009 18:06
> To: rubyonrails-core@googlegroups.com
> Subject: [Rails-core] Re: tracking_attribute_changes and using default
> checkbox form in the view

> I checked the test cases (dirty_test.rb), and it contains a test for
> this specifically which passes at least on my env.

> def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
> pirate = Pirate.new
> pirate.parrot_id = 0
> pirate.save!
> # other tests
> pirate.parrot_id = '0'
> assert !pirate.changed?
> end

> Could you check this as well, and otherwise try to extract a small
> piece of code, into a test or sample app, which shows the behaviour?
> If you do so, please file a ticket.

> - Eloy

>> You are right, I have a check box helper (by default, it's sending 0
>> or 1 ):wink: and also my checkbox is represented in my database by an
>> integer ==> 0 means unchecked , 1 means check.

>> So my form is sending 0 because my checkbox is unchecked and before
>> sending, the attribute of the object had the same value 0 (I checked
>> in the database to be sure)

>> So I don't understand why rails tell me the state of this attribute
>> change ? because the attribute is not changing ... 0 ==> 0

>> Best regards,
>> Antoine

>>> Sorry, I forgot to add the most important part :slight_smile:

>>> What might be happening is that you have a default value of nil
>>> (NULL)
>>> which is then changed to an empty string "". Because the browser
>>> sends
>>> a value because of the gotcha workaround.

>>> So the easiest solution would be to have a default value for the
>>> field.
>>> Which is something you want to have anyways for a boolean field IMO.

>>> - Eloy

>>>> Hi,

>>>> This was probably because you used the check_box helper, which
>>>> makes
>>>> sure the browser always sends a value.
>>>> See the documentation for the check_box helper:

>>>> === Gotcha

>>>> The HTML specification says unchecked check boxes are not
>>>> successful, and thus web browsers do not send them. Unfortunately
>>>> this introduces a gotcha: if an Invoice model has a paid flag, and
>>>> in the form that edits a paid invoice the user unchecks its check
>>>> box, no paid parameter is sent. So, any mass-assignment idiom like
>>>> @invoice.update_attributes(params[:invoice])
>>>> wouldn't update the flag.
>>>> To prevent this the helper generates a hidden field with the same
>>>> name as the checkbox after the very check box. So, the client
>>>> either
>>>> sends only the hidden field (representing the check box is
>>>> unchecked), or both fields. Since the HTML specification says key/

>> >>>> value pairs have to be sent in the same order they appear in the

>>>> form and Rails parameters extraction always gets the first
>>>> occurrence of any given key, that works in ordinary forms.

>>>> - Eloy

>>>>> Dear all,

>>>>> Maybe i wasn't so clear, excuse me for my english :wink:

>>>>> My problem is simple ..
>>>>> I didn't change the value of my checkboxes in my form but the core
>>>>> function of rails was saying that the values currently change !
>>>>> and
>>>>> that's why rails update the values in mysql... and also return a
>>>>> wrong
>>>>> information if i use the new fontions like : attributes_change,
>>>>> attribute_changed?...

>>>>> I don't understand why ?

>>>>> thank you for your help !
>>>>> Antoine

>>>>>> Dear all,

>>>>>> I get a stupid bug and i don't know if there is something wrong
>>>>>> in
>>>>>> rails or if it is me doing a stupid thing.

>>>>>> I have a checkbox called (ass_tg) in my form and when i saved my
>>>>>> form , i write in my model:

>>>>>> before_save :test

>>>>>> def test
>>>>>> puts "-----------------------"
>>>>>> puts ass_tg_change
>>>>>> puts "-------------------------"
>>>>>> end

>>>>>> I should have a nil answer but I get in my console :

>>>>>> -----------------------
>>>>>> 0
>>>>>> 0
>>>>>> -------------------------

>>>>>> And I have this attributes save in the SQL log ..(I didn't change
>>>>>> the
>>>>>> status of the checkbox ...)

>>>>>> UPDATE `companies` SET .... `ass_tg` = 0,...... WHERE `....

>>>>>> It seems like rails don't like integer attributes equal to 0
>>>>>> when it
>>>>>> execute all the change functions (change, changed?, was ...) ??

>>>>>> Best regards,

>>>>>> Thank you for your help,
>>>>>> Antoine

>

--
Cheers

Koz