I have a table in my db which saves a config as a yaml-file.
The respective column is called config.
I would like to read, let’s say, the first 5 config entries, and convert them back to yaml.
I tried in the rails console, as follows:
Person.pluck(:config).first(5).each do {|c| c.to_yaml}
This seems to be quite wrong, I know. The first thing that got me tripping:
- Why will
Person.first(5).pluck(:config)yield a NoMethodError? - Doing it the way I do it above will first pluck ALL configs and then retrieve the first 5 rows, correct? How can I speed up the query?
Second question:
- What would be the correct way, to iterate over the returned table rows (e.g. the first 5), and convert the content of each config-column back to yaml via the
to_yaml-method?
It yields a
NoMethodErrorbecause you are trying to send the messagepluckto an Array, which has no such method.Yes, that is correct. If you want to pluck the config out of just the first five records, use
limit:For info on how to parse the
configvalues as YAML, see this thread.