Dave Lynam
2007-Dec-13 09:22 UTC
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. ;) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2007-Dec-13 09:55 UTC
Re: anyone know how to add an element to a object instance v
Avatar.find(:all ...).collect{|avatar| @avatars << avatar} should work -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Dec-13 09:56 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
On Dec 13, 2007, at 10:22 AM, Dave Lynam wrote:> > 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. ;)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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-13 11:49 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
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 On Dec 13, 1:56 am, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Dec 13, 2007, at 10:22 AM, Dave Lynam wrote: > > > > > 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. ;) > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Dec-13 12:00 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
On Dec 13, 2007, at 12:49 PM, Dave Lynam wrote:> 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, DaveI 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-14 23:34 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
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. On Dec 13, 4:00 am, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Dec 13, 2007, at 12:49 PM, Dave Lynam wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Dec-14 23:59 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
On Dec 15, 2007, at 12:34 AM, Dave Lynam 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-15 00:24 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
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! On Dec 14, 3:59 pm, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Dec 15, 2007, at 12:34 AM, Dave Lynam 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Dec-15 00:48 UTC
Re: anyone know how to add an element to a object instance variable and not overwrite it?
On Dec 15, 2007, at 1:24 AM, Dave Lynam wrote:> 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 :admirerYou put those two in plural, right?> > endThe 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---