Forum: Ruby on Rails Strange thing with ActiveRecord ??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
95c4e5563b250ffb35f852f439f482b1?d=identicon&s=25 Nadav (Guest)
on 2006-01-19 09:47
(Received via mailing list)
Hi

I am probably missing something, so please help:

I have a table called 'zipped_responses' with an 'item_names' varchar
column.

I want to create a comma separated string of 'names'.
I have an 'add_item_names' method which takes a 'name' and put it at the
end
of a string with a preceding comma.

The model goes like this:

class ZippedResponse < ActiveRecord::Base

	def add_item_names (name)
	  str = read_attribute(:item_names)
	  str << "," unless str.empty?
          str << name
	  write_attribute(:item_names, str)
	end

end

Now in the console, I write

z=ZippedResponse.new
z.add_item_names("aa")

and I get:
z.item_names => "aa"

Fine until here. Now I write:

z1=ZippedResponse.new

and to my suprise I get that in z1, the 'item_names' attribute has also
the
value "aa" !!
(z1.item_name => "aa")

How come ??

Nadav
This topic is locked and can not be replied to.