I would like to make a string to become an attribute:
Model:
Author
attr_accessible :book1, book2, book3 etc...
I would like to retrieve 20 books in a lesser command
def do_something
self.book1
self.book2
self.book3
....
end
This is a solution I came up with but how can I make the string become an attribute so I could retrieve the data.
def do_something
count = 0
10.times do
self."book#{count += 1}"
end
end
This will work:
Also try this, it’s simpler and it should work as well:
self[:book1]orself['book1']BTW this is weird design. Consider using array of books instead: