ruby on rails - Is possible to append a key value to an ActiveRecord object -


i want have object has associated value added it.

this attempting do:

@users = @search.results  @user_results = []  @users.each |user|   @user_results = @user_results.push user << {photo_url: usersphoto.find_by_user_id(user.id).image_name} end 

i'm getting:

nomethoderror (undefined method `<<' #):

is there way this?

this associations for. should add association user usersphoto you're trying find, , use @user.users_photo.image_name.

class user < activerecord::base   has_one :users_photo end 

failing that, add photo_url method user model, wraps usersphoto.find...:

class user < activerecord::base    def photo_url     usersphoto.find_by_user_id(id).image_name   end end 

failing that, can you're trying do, you'll need add attr_accessor :photo_url user model, , syntax wrong:

class user < activerecord::base   attr_accessor :photo_url end  @users.each |user|   user.photo_url = usersphoto.find_by_user_id(user.id).image_name end 

Comments