ruby on rails - Difference in an attribute value when asking object directly vs iterating over all table entries -


i have weird anomaly specific object in table.

for ex. let's have user table has attribute birthday.

if iterate on user table without asking no attributes @ error because of invalid entry:

1.9.3-p551 :022 >   begin 1.9.3-p551 :022 >     user.all.map {|u| u } 1.9.3-p551 :023?>   rescue => e 1.9.3-p551 :024?>     puts "problem user #{u.id}, error: #{e}" 1.9.3-p551 :025?>   end   user load (267.5ms)  select `user`.* `user` problem user 2569, error: invalid date in field 'birthday': 1998-07-00  => nil 

now if ask directly current problematic object birthday, give me correct value correct data type:

1.9.3-p551 :027 > u = user.find(2569) 1.9.3-p551 :027 > u.birthday  => tue, 12 oct 2000  

attribute defined in schema:

 create_table "users", :force => true |t|   t.date      "birthday" 

your problem variable scope: within map block, u defined current iterated object, outside block u undefined.

then how come error output can give out u.id? might ask.

that because have u defined outside whole block (perhaps earlier testing).

observe code:

u = 1 puts "before: #{u}" 5.times{|u| puts u} puts "after: #{u}" 

output:

before: 1 0 1 2 3 4 after: 1 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -