I thought I understood attr_* and the shorthand notation for map(&:name.to_proc).join(' '), but I ran across this today.
Why does setting attr_accessor keep my from being able to write something like NaughtyWord.all.collect(&:word)
and instead require me to write out a much longer NaughtyWord.all.collect{|naughty_word| naughty_word["word"]}
class NaughtyWord < ActiveRecord::Base
attr_accessor :word, :must_review
validates_presence_of :word
def self.regex_filter
words = NaughtyWord.all.collect(&:word).join("|")
end
end
#irb(main):014:0> NaughtyWord.all.collect(&:word)
# NaughtyWord Load (0.4ms) SELECT `naughty_words`.* FROM `naughty_words`
#=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
#irb(main):024:0> NaughtyWord.last
# NaughtyWord Load (0.3ms) SELECT `naughty_words`.* FROM `naughty_words` ORDER BY `naughty_words`.`id` DESC LIMIT 1
#=> #<NaughtyWord id: 11, word: "word", must_review: true>
#irb(main):025:0> NaughtyWord.last.word
# NaughtyWord Load (0.6ms) SELECT `naughty_words`.* FROM `naughty_words` ORDER BY `naughty_words`.`id` DESC LIMIT 1
#=> nil
# irb(main):026:0> NaughtyWord.last["word"]
# NaughtyWord Load (0.6ms) SELECT `naughty_words`.* FROM `naughty_words` ORDER BY `naughty_words`.`id` DESC LIMIT 1
# => "word"
#irb(main):030:0> NaughtyWord.all.collect{|naughty_word| naughty_word["word"]}
# NaughtyWord Load (0.6ms) SELECT `naughty_words`.* FROM `naughty_words`
#=> ["word", "word", "word", "word", "word", "word", "word", "word", "word", "word", "word"]
Everything works if I comment out the attr_accessor line