# Anyone know how to add an element to a object instance variable and not overwrite it?

**URL:** <https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999>\
**Category:** Rails\
**Created:** [December 13, 2007, 10:23am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999 "2007-12-13T10:23:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 13, 2007, 10:23am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/1 "2007-12-13T10:23:23Z")

</div>

Heres the code Im using:  
for admirer in @admirers  
@avatars =||(??) Avatar.find(:all, :conditions =\> { :user\_id =\>  
admirer.admirer\_id, :thumbnail =\> ‘tiny’ }) end

I want to add a new Avatar object to @avatars for each admirer\_id and  
Im confused on the syntax to do this. Does this make sense at all?  
Anyone know how to do this? Please help if you have any ideas, Ive  
been looking all over online to try and figure this out but to no  
avail. Thanks. 😉

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 13, 2007, 10:55am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/2 "2007-12-13T10:55:35Z")

</div>

Avatar.find(:all …).collect{|avatar| @avatars \<\< avatar}

should work

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 13, 2007, 10:56am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/3 "2007-12-13T10:56:49Z")

</div>

On Dec 13, 2007, at 10:22 AM, Dave L. wrote:

> avail. Thanks. 😉  
> A way to append the contents of an array to another is to use +=, this  
> way:

@avatars += Avatar.find(…)

On the other hand that query looks non-idiomatic, and the column  
admirer.admirer\_id a bit suspicious, since it would normally be  
admirer.id (or admirer.admired\_id if you have there the ID of the who  
admirer admires).

Depending of the details you’d end up writing something like that  
looks like this:

@avatars += admirer.avatars.find\_all\_by\_thumbnail(‘tiny’)

– fxn

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 13, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/4 "2007-12-13T13:01:24Z")

</div>

On Dec 13, 2007, at 12:49 PM, Dave L. wrote:

> admired the profile. Do you have any ideas why I would be getting  
> this error. Thanks for your time, Dave

I would expect a user has\_many :admirers, and has\_many :avatars. On  
the other hand an avatar belongs\_to :user. With those relationships,  
if the entry point is an avatar you’d do this:

@admirers = avatar.user.admirers

Perhaps you could show an avatar but directly put the user id in the  
associated link… Does that make sense?

– fxn

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 13, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/5 "2007-12-13T12:49:36Z")

</div>

Thanks so much for the input. Ive been tinkering with the code and am  
getting this error:  
Unknown column ‘avatars.admirer\_id’ in ‘where clause’: SELECT \* FROM  
avatars WHERE (avatars.admirer\_id = 11)

The admirer model has a ‘has\_one :avatar’ relationship. Is there some  
other kind of relationship i need to define. To clarify the  
admirer\_id, I also store who the admirer admires as user\_id. My  
ultimate goal is to make a profile where someone can click “admire  
this person” and then that profile displays all the people who have  
admired the profile. Do you have any ideas why I would be getting  
this error. Thanks for your time, Dave

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 15, 2007, 1:01am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/6 "2007-12-15T01:01:12Z")

</div>

On Dec 15, 2007, at 12:34 AM, Dave L. wrote:

> Ive been working on this for the past couple days but no go. The  
> biggest confusion stems from the fact that I want to find all the  
> admirer\_id’s in the admirer table by matching them to the current  
> profile through the user\_id. So lets say the user\_id is 4, then I  
> search through the admirers table and find all the admirer\_id’s that  
> match a user\_id of 4. Then I want to take these admirer id’s and  
> create an @avatar object containing all the avatars that have their  
> user\_id column match the admirer\_id.

It would be useful to know the actual tables and column names, class  
definitions, and model relationships you have written. Please send  
this with plain code, not English. Strip everything that it is not  
relevant.

– fxn

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 15, 2007, 12:35am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/7 "2007-12-15T00:35:22Z")

</div>

Ive been working on this for the past couple days but no go. The  
biggest confusion stems from the fact that I want to find all the  
admirer\_id’s in the admirer table by matching them to the current  
profile through the user\_id. So lets say the user\_id is 4, then I  
search through the admirers table and find all the admirer\_id’s that  
match a user\_id of 4. Then I want to take these admirer id’s and  
create an @avatar object containing all the avatars that have their  
user\_id column match the admirer\_id. I think there must be some way  
to do this through creating indexes, but Im new to the database  
relationships so its not so straightforward. The way I had it set up  
above doesnt look scalable at all:  
for admirer in @admirers  
@avatars =||(??) Avatar.find(:all, :conditions =\> { :user\_id =\>  
admirer.admirer\_id, :thumbnail =\> ‘tiny’ }) end

because this is going to have to search the entire admirers table and  
then cross reference the entire avatar table, which canget very  
expensive with a lot of users. Do you have any ideas about how to  
implement indexes in this situation. Thanks for your help. Youve  
helped me a lot already.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 15, 2007, 1:27am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/8 "2007-12-15T01:27:29Z")

</div>

Here are the relevant tables and classes:

Admirers Table : id, user\_id, admirer\_id (the admirer\_id is the one  
that needs to be displayed by creating an avatar object with the  
corresponding user\_id)

class Admirer \< ActiveRecord::Base  
belongs\_to :user  
end

Avatars Table: id, user\_id, content\_type, thumbnail, size, width,  
height (uses attachment\_fu)  
class Avatar \< ActiveRecord::Base  
belongs\_to :user  
end

Users Table: id, screen\_name, email, password, gender  
class User \< ActiveRecord::Base  
has\_one :photo  
has\_many :avatar  
has\_many :admirer  
end

thanks so much!

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [December 15, 2007, 1:49am UTC](https://www.ruby-forum.com/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/122999/9 "2007-12-15T01:49:41Z")

</div>

On Dec 15, 2007, at 1:24 AM, Dave L. wrote:

> class Avatar \< ActiveRecord::Base  
> belongs\_to :user  
> end
> 
> Users Table: id, screen\_name, email, password, gender  
> class User \< ActiveRecord::Base  
> has\_one :photo  
> has\_many :avatar  
> has\_many :admirer

You put those two in plural, right?

> end

The problem you are facing comes from the fact that there’s no link  
between admirers and avatars, yet you want to join them.

This in turn is consequence of what seems a not yet rounded model: to  
admire and being admired is a relationship between _users_. That’s a  
hands\_and\_belongs\_to\_many linking users to users, you need to link  
them both ways.

The admirer’s table would have as columns: “admirer\_id”, “admired\_id”,  
and NO column “id” (create\_table :admirers, :id =\> false do |t| …).

In the User model you have

has\_and\_belongs\_to\_many :admirers, …, :join\_table =\> ‘admirers’  
has\_and\_belongs\_to\_many :admireds, …, :join\_table =\> ‘admirers’

Check the API for the options in the ellipsis, Google for “self-  
referential has\_and\_belongs\_to\_many” as well.

Then you have a link from users to avatars going through admirers,  
because

user.admirers

is a collection of users, and they have avatars.

– fxn
