undefined method `[]=' for #<ActiveRecord::Result:0x007ff34c76d508>
dylanjha opened this issue · 7 comments
dylanjha commented
rails 4, out of the box running either:
bundle exec rake db:data:dump
bundle exec rake db:data:dump_dir
wtfiwtz commented
Same here...
wtfiwtz commented
def self.unhash_records(records, keys)
records.each_with_index do |record, index|
records[index] = unhash(record, keys)
end
records
end
The 'records' parameter is passed in from ActiveRecord (v4.0.0) as a ActiveRecord::Result, not an array
Cheers,
Nigel
wtfiwtz commented
Here's a diff from Rails 3.2.13 to 4.0.0:
Nigel-MBP:gems wtfiwtz$ diff activerecord-3.2.13/lib/active_record/result.rb activerecord-4.0.0/lib/active_record/result.rb
11c11
< attr_reader :columns, :rows
---
> attr_reader :columns, :rows, :column_types
13,16c13,17
< def initialize(columns, rows)
< @columns = columns
< @rows = rows
< @hash_rows = nil
---
> def initialize(columns, rows, column_types = {})
> @columns = columns
> @rows = rows
> @hash_rows = nil
> @column_types = column_types
26a28,53
> alias :map! :map
> alias :collect! :map
>
> # Returns true if there are no records.
> def empty?
> rows.empty?
> end
>
> def to_ary
> hash_rows
> end
>
> def [](idx)
> hash_rows[idx]
> end
>
> def last
> hash_rows.last
> end
>
> def initialize_copy(other)
> @columns = columns.dup
> @rows = rows.dup
> @hash_rows = nil
> end
>
32c59
< # used as keys in ActiveRecord::Model's @attributes hash
---
> # used as keys in ActiveRecord::Base's @attributes hash
Maybe the definition of the [] method is messing with the array dereferencing?
wtfiwtz commented
Okay, I added this to the following file .rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/result.rb (ActiveRecord::Result) -
It fixes the issue...
def []=(idx,value)
hash_rows
@hash_rows[idx] = value
value
end
Should we add this as a bug to the Rails 4.x project?
ChrisZou commented
👍