some.hacker
some.hacker

Reputation: 35

ActiveRecord not updating attribute

Here is a code snippet and its resulting output. The code appears to update the attribute, but when I check the record manually in the database, there is no change. This is confirmed by .changed? returning false. What have I done wrong?

class Ticket < ActiveRecord::Base
##+-----------------+-----------------------+------+-----+---------+-------+
##| Field           | Type                  | Null | Key | Default | Extra |
##+-----------------+-----------------------+------+-----+---------+-------+
##| ticketid        | bigint(20) unsigned   | NO   | PRI | NULL    |       |
##| ticketnumber    | bigint(20) unsigned   | NO   |     | NULL    |       |
##| contactname     | char(40)              | YES  |     | NULL    |       |
##| department      | char(40)              | YES  |     | NULL    |       |
##| tech            | char(40)              | YES  |     | NULL    |       |
##| timeopened      | char(18)              | YES  |     | NULL    |       |
##| timelastchanged | char(18)              | YES  |     | NULL    |       |
##| mintuesopen     | mediumint(8) unsigned | YES  |     | NULL    |       |
##| searchtermlist  | varchar(255)          | YES  |     | NULL    |       |
##+-----------------+-----------------------+------+-----+---------+-------+

attr_accessible :searchtermlist, :minutesopen, :timelastchanged, :tech

end

....

thisticket = Ticket.find_by_ticketid(ticketid)
    if thisticket != nil
        puts "---Ticket #{ticketid} Found!"
        if thisticket.searchtermlist.include? importedsearchtermlist
            puts "---Search term list current! Skipping..."
        else
            puts "---Updating search term list for ticket #{ticketid}"
            puts importedsearchtermlist
            puts thisticket.ticketid
            puts thisticket.searchtermlist
            updatedsearchtermlist = thisticket.searchtermlist << "," << importedsearchtermlist
            puts updatedsearchtermlist
            thisticket.searchtermlist = updatedsearchtermlist
            result = thisticket.save!
            puts result
            puts thisticket.changed?
            puts thisticket.searchtermlist
            sleep(60)
        end

And the output:

---Ticket 47048 Found!
---Updating search term list for ticket 47048
virus
47048
update
update,virus
true
false
update,virus

Upvotes: 0

Views: 116

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10147

Using << is not changing the attribute value. You can check using thisticket.changes returns hash in which doesn't contain changed value. So try using:

updatedsearchtermlist += ( ',' + importedsearchtermlist)

or:

updatedsearchtermlist = [updatedsearchtermlist, importedsearchtermlist].join(',')

instead of:

updatedsearchtermlist = thisticket.searchtermlist << "," << importedsearchtermlist

After fixing also changed? will return false(In your debug result). Because, you are checking it after saving the record. So it will always return false.

Upvotes: 1

Related Questions