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

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. :wink:

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

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

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

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.

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

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!

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

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