0

I would like to dynamically add attributes to a Ruby on Rails object so I can access them with an Ajax call. I understand that I can send the info with another Ajax call, but I would much prefer to add the :first_name and :avatar_url attributes dynamically. Here is my code...

def get_info

    comments = []
    allTranslations.each do |trans|

        if trans.comments.exists?
            trans.comments.each do |transComment|

                user = ...
                    class << transComment
                        attr_accessor :first_name
                        attr_accessor :avatar_url
                    end
                transComment.first_name = user.first_name
                transComment.avatar_url = user.avatar.url

                comments.push(transComment)


                puts("trans user comments info")
                transComments.each do |x|

                    puts x['comment']
                    puts x['first_name']
                    puts x.first_name
                    puts x['avatar_url']

                end
            end 
        end
    end

    @ajaxInfo = {
        translationUsers: allTranslations,
        currentUserId: @current_user.id,
        transComments: transComments

    }

    render json: @ajaxInfo

end

Out of the 4 print statements, only puts x.first_name prints, and none of the attributes are added to the objects when I log the results on my console.

Here is the corresponding Javascript and Ajax:

 $('.my-translations').click(function(){
    $('#translation').empty();



    getTranslations(id).done(function(data){
        console.log(data)    
        var transUsers = []

        ...

  });
 });

function getTranslations(translationId)   {
    return $.ajax({ 
        type: "GET",
        url: '/get_translations_users',
        data: {
            translationId: translationId
        },
        success: function(result)   {
            return result;
        },
        error: function(err)    {
            console.log(err);
        }
    });
};

Any tips or advice is appreciated! Thanks :)

rachster
  • 51
  • 4

2 Answers2

0

Out of the 4 print statements, only puts x.first_name prints

This is because when you call x['comment'] etc you call the [] method on x object and I don't think that the object is an hash. when you call .first_name you use the new attr_accessor created dynamically; i think also . avatar_url should work.

Does it work if you do instead:

  @ajaxInfo = {
    translationUsers: allTranslations,
    currentUserId: @current_user.id,
    transComments: comments

}
Jacopo Beschi
  • 178
  • 2
  • 6
  • Thank you for all your help!!! I found a great explanation and fix here: https://stackoverflow.com/questions/18429274/how-to-add-new-attribute-to-activerecord ... As it turns out, `attr_accessor` creates properties, not hashes and therefore it uses the `.` syntax. I ended up using the solution by Chris Kerlin. – rachster Aug 02 '17 at 15:54
0

I found this awesome thread that answers my questions: How to add new attribute to ActiveRecord

As stated by @CuriousMind attr_accessor creates properties, not hashes.

I solved this issue by following the solution by @Chris Kerlin

Thanks!

rachster
  • 51
  • 4