I have a hash that looks like this:
extras = {'facebook' => '', 'twitter' => ''}
And my User attributes are like this:
class User < ActiveRecord::Base
attr_accessible :facebook, :twitter, #...
I would like to retrieve the data defined in my extras hash in the following fashion (or a better oen – that is the one that came up to my mind)
extras.each do |k,v|
extras[k] = @user.k
end
Is this even possible ?
Yes, you can do that with the
.send(:method)or the.try(:method):You can do that to gather the user’s info:
Or with
.try(), which is safer: it will not raise an NoMethodError in case of unknown key sent to user:You can do it with a map also:
I made a post about this code, you can see it here:
http://rails.co.nf/?p=31