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

**URL:** <https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658>\
**Category:** rubyonrails-talk\
**Created:** [December 13, 2007, 9:22am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658 "2007-12-13T09:22:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [December 13, 2007, 9:22am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/1 "2007-12-13T09:22:42Z")

</div>

Heres the code Im using: for admirer in @admirers &nbsp;&nbsp;@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:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [December 13, 2007, 9:56am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/2 "2007-12-13T09:56:16Z")

</div>

A way to append the contents of an array to another is to use +=, this way:

&nbsp;&nbsp;&nbsp;@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:

&nbsp;&nbsp;&nbsp;@avatars += admirer.avatars.find\_all\_by\_thumbnail('tiny')

-- fxn

---

<div class="post-metadata">

**Author:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [December 13, 2007, 11:49am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/3 "2007-12-13T11:49:01Z")

</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:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [December 13, 2007, 12:00pm UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/4 "2007-12-13T12:00:48Z")

</div>

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:

&nbsp;&nbsp;&nbsp;@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:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [December 14, 2007, 11:34pm UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/5 "2007-12-14T23:34:21Z")

</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 &nbsp;&nbsp;@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:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [December 14, 2007, 11:59pm UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/6 "2007-12-14T23:59:53Z")

</div>

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:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [December 15, 2007, 12:24am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/7 "2007-12-15T00:24:40Z")

</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 &nbsp;&nbsp;belongs\_to :user end

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

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

thanks so much!

---

<div class="post-metadata">

**Author:** ![fxn](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fxn/32/12956_2.png) [@fxn](https://discuss.rubyonrails.org/u/fxn)\
**Post date:** [December 15, 2007, 12:48am UTC](https://discuss.rubyonrails.org/t/anyone-know-how-to-add-an-element-to-a-object-instance-variable-and-not-overwrite-it/21658/8 "2007-12-15T00:48:48Z")

</div>

> 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 &nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :admirers, ..., :join\_table =\> 'admirers' &nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;user.admirers

is a collection of users, and they have avatars.

-- fxn
