attr_ *とmap(&:name.to_proc).join('')の省略表記を理解したと思いましたが、今日これに遭遇しました。
attr_accessorを設定すると、次のようなものを書き込めNaughtyWord.all.collect(&:word)
なくなり、代わりにもっと長く書き出す必要があるのはなぜですか。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"]
attr_accessor行をコメントアウトすると、すべてが機能します