Dave Lynam
2007-Dec-21 18:30 UTC
please someone help with this active record question! its driving me crazy
Im having trouble developing the database structure for the following scenario. I have a table that only contains 2 fields for each object. It has a admirer_id and an admired_id. There is also a user table. Now all I want to do is go through the admirers table and for each admired_id, take the corresponding admirer_id and collect all the users that have a corresponding user_id to this admirer_id. Im having trouble setting up the database relationships and how I should go about this in general. I think this may involve using a self- referential has_and_belongs_to_many relationship? Any help/advice is greatly appreciated. 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 -~----------~----~----~----~------~----~------~--~---
gemblon (t.b.)
2007-Dec-21 19:27 UTC
Re: please someone help with this active record question! i
Dave Lynam wrote:> Im having trouble developing the database structure for the following > scenario. I have a table that only contains 2 fields for each > object. It has a admirer_id and an admired_id. There is also a user > table. Now all I want to do is go through the admirers table and for > each admired_id, take the corresponding admirer_id and collect all the > users that have a corresponding user_id to this admirer_id. Im having > trouble setting up the database relationships and how I should go > about this in general. I think this may involve using a self- > referential has_and_belongs_to_many relationship? Any help/advice is > greatly appreciated. Thanks! ;)not being a brain in the group, here is some general information, to be supplimented by others. OLD WAY - has_and_belongs_to_many :my_model This is an out of date technique to set up the many-to-many table. A major limitation to this method is that you cannot have any more fields in the table then the 2 joining fields, and the join table does not use a primary field key called id. You cant sort joined fields very easily with this method. This technique is very easy to set up. The table name is the plural names of the 2 joining tables, in alphabetical order, separated by an underscore. Table C and B would be BS_CS. The only 2 fields in the join table would be b_id and c_id. NEW WAY - has_many :through This technique allows the join table to have as many fields as you want. You need a model, for the joining, to make this work. You can just generate a model. But if you need more fields, you can scaffold. The join table also gets the primary key field (id) that is automatically generated in most cases. The joining table can be named what ever you want. The current trend is to use a table name that just has one word, and usually ends in -ship, -tion, or -ment. There can be endless hours of debate over the name of the joining table. You will need to have the 2 joining tables foreign key fields in this table, like the fashion <tablename>_id, that are not pluralized. If you wanted to join table A and table B, an example of the joining table could be: TABLES A id (integer, primary key) name (string) B id (integer, primary key) name (string) AB (many table for joining tables A and B) id (integer, primary key) a_id (integer) b_id (integer) (more fields) The MODELS A has_many :ABs has_many :Bs, :through => :ABs AB belongs_to :A belongs_to :B B has_many :ABs has_many :As, :through => :ABs SOME OPTIONS has_many :mytable, :foreign_key => ''myfield_id'', :class_name => ''myclassname'', :dependent => :destroy -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-21 23:07 UTC
Re: please someone help with this active record question! i
I would disagree that habtm is out of date. The limitation of not being able to add columns to the join table is only a limitation if that''s what you need want to do. Otherwise, I think habtm is TSTTCPW. ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-22 09:20 UTC
Re: please someone help with this active record question! its driving me crazy
Can one user admire many other users? Perhaps have another table outside of users called admirees_admirers and store the admiree_id and admirer_id in there. Reference via the user model like this: class User < ActiveRecord::Base has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", :foreign_key => "admiree_id" has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", :foreign_key => "admirer_id" end On Dec 22, 2007 5:00 AM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Im having trouble developing the database structure for the following > scenario. I have a table that only contains 2 fields for each > object. It has a admirer_id and an admired_id. There is also a user > table. Now all I want to do is go through the admirers table and for > each admired_id, take the corresponding admirer_id and collect all the > users that have a corresponding user_id to this admirer_id. Im having > trouble setting up the database relationships and how I should go > about this in general. I think this may involve using a self- > referential has_and_belongs_to_many relationship? Any help/advice is > greatly appreciated. Thanks! ;) > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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-22 10:12 UTC
Re: please someone help with this active record question! its driving me crazy
So using this setup of another admirees_admirers table, how would you gather the admirer user objects? would it just be @admirers user.admirers.find(:all)? Im unclear how, once you find the admiree, you then find the corresponding admirers. On Dec 22, 1:20 am, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can one user admire many other users? Perhaps have another table outside of > users called admirees_admirers and store the admiree_id and admirer_id in > there. > > Reference via the user model like this: > > class User < ActiveRecord::Base > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > :foreign_key => "admiree_id" > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > :foreign_key => "admirer_id" > end > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Im having trouble developing the database structure for the following > > scenario. I have a table that only contains 2 fields for each > > object. It has a admirer_id and an admired_id. There is also a user > > table. Now all I want to do is go through the admirers table and for > > each admired_id, take the corresponding admirer_id and collect all the > > users that have a corresponding user_id to this admirer_id. Im having > > trouble setting up the database relationships and how I should go > > about this in general. I think this may involve using a self- > > referential has_and_belongs_to_many relationship? Any help/advice is > > greatly appreciated. Thanks! ;) > > -- > Ryan Bigghttp://www.frozenplague.net--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-22 10:25 UTC
Re: please someone help with this active record question! its driving me crazy
@user.admirers SHOULD be enough. On Dec 22, 2007 8:42 PM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > So using this setup of another admirees_admirers table, how would you > gather the admirer user objects? would it just be @admirers > user.admirers.find(:all)? Im unclear how, once you find the admiree, > you then find the corresponding admirers. > > On Dec 22, 1:20 am, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Can one user admire many other users? Perhaps have another table outside > of > > users called admirees_admirers and store the admiree_id and admirer_id > in > > there. > > > > Reference via the user model like this: > > > > class User < ActiveRecord::Base > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > :foreign_key => "admiree_id" > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > :foreign_key => "admirer_id" > > end > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Im having trouble developing the database structure for the following > > > scenario. I have a table that only contains 2 fields for each > > > object. It has a admirer_id and an admired_id. There is also a user > > > table. Now all I want to do is go through the admirers table and for > > > each admired_id, take the corresponding admirer_id and collect all the > > > users that have a corresponding user_id to this admirer_id. Im having > > > trouble setting up the database relationships and how I should go > > > about this in general. I think this may involve using a self- > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > greatly appreciated. Thanks! ;) > > > > -- > > Ryan Bigghttp://www.frozenplague.net > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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-23 16:36 UTC
Re: please someone help with this active record question! its driving me crazy
Ok thanks. Im making some progress, but I get this error: Mysql::Error: Not unique table/alias: ''admirers'': SELECT * FROM admirers INNER JOIN admirers ON admirers.id = admirers.admirer_id WHERE (admirers.admiree_id = 4 ) any idea what this means? On Dec 22, 2:25 am, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @user.admirers SHOULD be enough. > > On Dec 22, 2007 8:42 PM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > So using this setup of another admirees_admirers table, how would you > > gather the admirer user objects? would it just be @admirers > > user.admirers.find(:all)? Im unclear how, once you find the admiree, > > you then find the corresponding admirers. > > > On Dec 22, 1:20 am, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Can one user admire many other users? Perhaps have another table outside > > of > > > users called admirees_admirers and store the admiree_id and admirer_id > > in > > > there. > > > > Reference via the user model like this: > > > > class User < ActiveRecord::Base > > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > > :foreign_key => "admiree_id" > > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > > :foreign_key => "admirer_id" > > > end > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Im having trouble developing the database structure for the following > > > > scenario. I have a table that only contains 2 fields for each > > > > object. It has a admirer_id and an admired_id. There is also a user > > > > table. Now all I want to do is go through the admirers table and for > > > > each admired_id, take the corresponding admirer_id and collect all the > > > > users that have a corresponding user_id to this admirer_id. Im having > > > > trouble setting up the database relationships and how I should go > > > > about this in general. I think this may involve using a self- > > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > > greatly appreciated. Thanks! ;) > > > > -- > > > Ryan Bigghttp://www.frozenplague.net > > -- > Ryan Bigghttp://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2007-Dec-23 17:11 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 23, 2007, at 10:36 AM, Dave Lynam wrote:> > Ok thanks. Im making some progress, but I get this error: > Mysql::Error: Not unique table/alias: ''admirers'': SELECT * FROM > admirers INNER JOIN admirers ON admirers.id = admirers.admirer_id > WHERE (admirers.admiree_id = 4 ) > any idea what this means? >When you reference the same table in a query multiple times, you must alias each: select * from admirers a1 inner join admirers a2 on a2.id = a1.someotherid Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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-23 23:51 UTC
Re: please someone help with this active record question! its driving me crazy
How does this translate to ruby (active record) code? Thanks man, Dave On Dec 23, 9:11 am, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 23, 2007, at 10:36 AM, Dave Lynam wrote: > > > > > Ok thanks. Im making some progress, but I get this error: > > Mysql::Error: Not unique table/alias: ''admirers'': SELECT * FROM > > admirers INNER JOIN admirers ON admirers.id = admirers.admirer_id > > WHERE (admirers.admiree_id = 4 ) > > any idea what this means? > > When you reference the same table in a query multiple times, you must > alias each: > > select * > from admirers a1 > inner join admirers a2 > on a2.id = a1.someotherid > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2007-Dec-24 00:12 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 23, 2007, at 5:51 PM, Dave Lynam wrote:> > How does this translate to ruby (active record) code? Thanks man, > Dave >Ah, well, the down side to participating in this group by email is I don''t always have a proper context for answering a question. I thought you were talking about a SQL query. Sorry. I''ll give it a little thought, but I''m thinking Ryan will probably come up with something before I will. Assuming he comes out of that Javascript dream, that is. :) Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-24 00:18 UTC
Re: please someone help with this active record question! its driving me crazy
Ryan thought his syntax was correct, AGAIN! I see what it''s doing and Phillip is right about the SQL. I''ll have a shot at it during my lunch break (in an hour or two), and I''ll get back to you. On Dec 24, 2007 10:42 AM, Phillip Koebbe <phillipkoebbe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Dec 23, 2007, at 5:51 PM, Dave Lynam wrote: > > > > > How does this translate to ruby (active record) code? Thanks man, > > Dave > > > > Ah, well, the down side to participating in this group by email is I > don''t always have a proper context for answering a question. I > thought you were talking about a SQL query. Sorry. I''ll give it a > little thought, but I''m thinking Ryan will probably come up with > something before I will. Assuming he comes out of that Javascript > dream, that is. :) > > Peace, > Phillip > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-24 03:36 UTC
Re: please someone help with this active record question! its driving me crazy
Alrighty, didn''t get the time off I expected during my lunchbreak (went out for lunch, for around 2 hours :)) so I''ll do it tonight probably. On Dec 24, 2007 10:48 AM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ryan thought his syntax was correct, AGAIN! > > I see what it''s doing and Phillip is right about the SQL. I''ll have a shot > at it during my lunch break (in an hour or two), and I''ll get back to you. > > > > On Dec 24, 2007 10:42 AM, Phillip Koebbe <phillipkoebbe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On Dec 23, 2007, at 5:51 PM, Dave Lynam wrote: > > > > > > > > How does this translate to ruby (active record) code? Thanks man, > > > Dave > > > > > > > Ah, well, the down side to participating in this group by email is I > > don''t always have a proper context for answering a question. I > > thought you were talking about a SQL query. Sorry. I''ll give it a > > little thought, but I''m thinking Ryan will probably come up with > > something before I will. Assuming he comes out of that Javascript > > dream, that is. :) > > > > Peace, > > Phillip > > > > > > > > > > > -- > Ryan Bigg > > http://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email. >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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-24 08:08 UTC
Re: please someone help with this active record question! its driving me crazy
whenvever you get a chance. I appreciate it. Thanks. :) On Dec 23, 7:36 pm, "Ryan Bigg" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Alrighty, didn''t get the time off I expected during my lunchbreak (went out > for lunch, for around 2 hours :)) so I''ll do it tonight probably. > > On Dec 24, 2007 10:48 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Ryan thought his syntax was correct, AGAIN! > > > I see what it''s doing and Phillip is right about the SQL. I''ll have a shot > > at it during my lunch break (in an hour or two), and I''ll get back to you. > > > On Dec 24, 2007 10:42 AM, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On Dec 23, 2007, at 5:51 PM, Dave Lynam wrote: > > > > > How does this translate to ruby (active record) code? Thanks man, > > > > Dave > > > > Ah, well, the down side to participating in this group by email is I > > > don''t always have a proper context for answering a question. I > > > thought you were talking about a SQL query. Sorry. I''ll give it a > > > little thought, but I''m thinking Ryan will probably come up with > > > something before I will. Assuming he comes out of that Javascript > > > dream, that is. :) > > > > Peace, > > > Phillip > > > -- > > Ryan Bigg > > >http://www.frozenplague.net > > Feel free to add me to MSN and/or GTalk as this email. > > -- > Ryan Bigghttp://www.frozenplague.net > Feel free to add me to MSN and/or GTalk as this email.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-25 01:12 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 21, 10:30 am, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Im having trouble developing the database structure for the following > scenario. I have a table that only contains 2 fields for each > object. It has a admirer_id and an admired_id. There is also a user > table. Now all I want to do is go through the admirers table and for > each admired_id, take the corresponding admirer_id and collect all the > users that have a corresponding user_id to this admirer_id.Try this: class User < ActiveRecord::Base has_and_belongs_to_many :admirers, :class_name => ''User'', :join_table => ''admirees_admirers'', :foreign_key => ''admiree_id'', :association_foreign_key => ''admirer_id'' has_and_belongs_to_many :admirees, :class_name => ''User'', :join_table => ''admirees_admirers'', :foreign_key => ''admirer_id'', :association_foreign_key => ''admiree_id'' end ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-25 12:12 UTC
Re: please someone help with this active record question! its driving me crazy
HABTM is pretty much deprecated compared to has_many :through. The proper way to do this is something like: class User < ActiveRecord::Base has_many :admirations has_many :admirers, :through => :admirations has_many :admirees, :through => :admirations end class Admiration < ActiveRecord::Base belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" end I think that should work (too late to plug it in and test it)... The advantage of HM:T is that you can add extra data to the relationship. For example, you could put "Admired since XX-XX-XXXX" and plugin the created_at attribute on the Admiration, or you could add in a field like "knows_admiree" and put something like "Knows and admires (x)." --Jeremy On Dec 22, 2007 4:20 AM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can one user admire many other users? Perhaps have another table outside of > users called admirees_admirers and store the admiree_id and admirer_id in > there. > > Reference via the user model like this: > > class User < ActiveRecord::Base > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > :foreign_key => "admiree_id" > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > :foreign_key => "admirer_id" > end > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Im having trouble developing the database structure for the following > > scenario. I have a table that only contains 2 fields for each > > object. It has a admirer_id and an admired_id. There is also a user > > table. Now all I want to do is go through the admirers table and for > > each admired_id, take the corresponding admirer_id and collect all the > > users that have a corresponding user_id to this admirer_id. Im having > > trouble setting up the database relationships and how I should go > > about this in general. I think this may involve using a self- > > referential has_and_belongs_to_many relationship? Any help/advice is > > greatly appreciated. Thanks! ;) > > http://www.frozenplague.net > > > > > > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-25 15:03 UTC
Re: please someone help with this active record question! its driving me crazy
Thanks, it looks like Im getting closer, but I am still getting an error: Mysql::Error: Unknown column ''admirations.user_id'' in ''where clause'': SELECT users.* FROM users INNER JOIN admirations ON users.id admirations.admirer_id WHERE ((admirations.user_id = 1)) On Dec 25, 4:12 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> HABTM is pretty much deprecated compared to has_many :through. The > proper way to do this is something like: > > class User < ActiveRecord::Base > has_many :admirations > has_many :admirers, :through => :admirations > has_many :admirees, :through => :admirations > end > > class Admiration < ActiveRecord::Base > belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" > belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" > end > > I think that should work (too late to plug it in and test it)... > > The advantage of HM:T is that you can add extra data to the > relationship. For example, you could put "Admired since XX-XX-XXXX" > and plugin the created_at attribute on the Admiration, or you could > add in a field like "knows_admiree" and put something like "Knows and > admires (x)." > > --Jeremy > > On Dec 22, 2007 4:20 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Can one user admire many other users? Perhaps have another table outside of > > users called admirees_admirers and store the admiree_id and admirer_id in > > there. > > > Reference via the user model like this: > > > class User < ActiveRecord::Base > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > :foreign_key => "admiree_id" > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > :foreign_key => "admirer_id" > > end > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Im having trouble developing the database structure for the following > > > scenario. I have a table that only contains 2 fields for each > > > object. It has a admirer_id and an admired_id. There is also a user > > > table. Now all I want to do is go through the admirers table and for > > > each admired_id, take the corresponding admirer_id and collect all the > > > users that have a corresponding user_id to this admirer_id. Im having > > > trouble setting up the database relationships and how I should go > > > about this in general. I think this may involve using a self- > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > greatly appreciated. Thanks! ;) > > >http://www.frozenplague.net > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-25 16:28 UTC
Re: please someone help with this active record question! its driving me crazy
Looks like maybe that was my fault...try this. :) class User < ActiveRecord::Base has_many :admirations, :foreign_key => ''admirer_id'', :class_name => ''Admiration'' has_many :admirees, :through => :admirations # Bad association name but you can name it whatever has_many :admireds, :foreign_key => ''admiree_id'', :class_name => ''Admiration'' has_many :admirers, :through => :admireds end class Admiration < ActiveRecord::Base belongs_to :admirer, :class_name => "User" belongs_to :admiree, :class_name => "User" end I haven''t tried this either but now that my brain is more awake, I think this should work. :) --Jeremy On Dec 25, 2007 10:03 AM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks, it looks like Im getting closer, but I am still getting an > error: > Mysql::Error: Unknown column ''admirations.user_id'' in ''where clause'': > SELECT users.* FROM users INNER JOIN admirations ON users.id > admirations.admirer_id WHERE ((admirations.user_id = 1)) > > On Dec 25, 4:12 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > HABTM is pretty much deprecated compared to has_many :through. The > > proper way to do this is something like: > > > > class User < ActiveRecord::Base > > has_many :admirations > > has_many :admirers, :through => :admirations > > has_many :admirees, :through => :admirations > > end > > > > class Admiration < ActiveRecord::Base > > belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" > > belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" > > end > > > > I think that should work (too late to plug it in and test it)... > > > > The advantage of HM:T is that you can add extra data to the > > relationship. For example, you could put "Admired since XX-XX-XXXX" > > and plugin the created_at attribute on the Admiration, or you could > > add in a field like "knows_admiree" and put something like "Knows and > > admires (x)." > > > > --Jeremy > > > > On Dec 22, 2007 4:20 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > Can one user admire many other users? Perhaps have another table outside of > > > users called admirees_admirers and store the admiree_id and admirer_id in > > > there. > > > > > Reference via the user model like this: > > > > > class User < ActiveRecord::Base > > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > > :foreign_key => "admiree_id" > > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > > :foreign_key => "admirer_id" > > > end > > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Im having trouble developing the database structure for the following > > > > scenario. I have a table that only contains 2 fields for each > > > > object. It has a admirer_id and an admired_id. There is also a user > > > > table. Now all I want to do is go through the admirers table and for > > > > each admired_id, take the corresponding admirer_id and collect all the > > > > users that have a corresponding user_id to this admirer_id. Im having > > > > trouble setting up the database relationships and how I should go > > > > about this in general. I think this may involve using a self- > > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > > greatly appreciated. Thanks! ;) > > > >http://www.frozenplague.net > > > > --http://www.jeremymcanally.com/ > > > > My books: > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-25 17:38 UTC
Re: please someone help with this active record question! its driving me crazy
Jeremy, habtm is only "deprecated" if you need additional information in the join table. If you don''t, then setting up a model for the join is unnecessary. Dave, the code I posted works with habtm. It''s the canonical way to do self-referential many-to-many relationships. ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-25 19:53 UTC
Re: please someone help with this active record question! its driving me crazy
No, it is not the "canonical" way. Maybe if you like to use out of date practices, and, if so, perhaps you''d like a little dynamic scaffolding to go with that HABTM? ;) HABTM is, and has been for some time, considered bad practice compared to HMT: http://www.slideshare.net/rabble/introduction-to-active-record-at-mysql-conference-2007/38 http://onrails.org/articles/2006/11/18/the-rails-edge-conference-in-denver-day-3 (Trying to find more information on this presentation...I had a link...) http://blog.hasmanythrough.com/2006/4/2/rich-associations-out-join-models-in The Rails Way by Obie Fernandez says this: "Use of habtm, which was one of the original innovative features in Rails, fell out of favor once the ability to create real join models was introduced via the has_many :through association." Of course, you can still use it and it''s still valid, but I wouldn''t say it''s "canonical" or even good practice. --Jeremy On Dec 25, 2007 12:38 PM, Mark Wilden <mark-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> > Jeremy, habtm is only "deprecated" if you need additional information > in the join table. If you don''t, then setting up a model for the join > is unnecessary. > > Dave, the code I posted works with habtm. It''s the canonical way to do > self-referential many-to-many relationships. > > > ///ark > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-25 20:29 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 25, 11:53 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No, it is not the "canonical" way.I used that term since it''s the method that''s described in Chad Fowler''s "Rails Recipes." Now, some may say that Fowler''s book is out of date, but habtm certainly does seem like the simplest way to accomplish this task.> HABTM is, and has been for some time, considered bad practice compared to HMT:Yes, I''ve heard what people say. All I get out of it is that modelling join tables allows you to add information to the join. I don''t understand why that would be important if you don''t need to do that (and YAGNI). It''s not agile.> Of course, you can still use it and it''s still valid, but I wouldn''t > say it''s "canonical" or even good practice.It works, and it''s the simplest way to solve a problem like the OP''s. ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-25 20:51 UTC
Re: please someone help with this active record question! its driving me crazy
Perfect! This works, you are the man. Thank you. I can now collect all the user_id''s of the admirers of a user by using @user.admireds. Now if I can only trouble you for some more brain power, I am trying to use these user_id''s to create an instance of @avatars. There is a user_id field in each avatar object and I want to gather all the avatar objects in @avatars that correspond to to the @user.admireds. I think this may be simple, but it is still giving me trouble. Any thoughts?? Thanks! On Dec 25, 8:28 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Looks like maybe that was my fault...try this. :) > > class User < ActiveRecord::Base > has_many :admirations, :foreign_key => ''admirer_id'', :class_name => > ''Admiration'' > has_many :admirees, :through => :admirations > > # Bad association name but you can name it whatever > has_many :admireds, :foreign_key => ''admiree_id'', :class_name => ''Admiration'' > has_many :admirers, :through => :admireds > end > > class Admiration < ActiveRecord::Base > belongs_to :admirer, :class_name => "User" > belongs_to :admiree, :class_name => "User" > end > > I haven''t tried this either but now that my brain is more awake, I > think this should work. :) > > --Jeremy > > On Dec 25, 2007 10:03 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Thanks, it looks like Im getting closer, but I am still getting an > > error: > > Mysql::Error: Unknown column ''admirations.user_id'' in ''where clause'': > > SELECT users.* FROM users INNER JOIN admirations ON users.id > > admirations.admirer_id WHERE ((admirations.user_id = 1)) > > > On Dec 25, 4:12 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > HABTM is pretty much deprecated compared to has_many :through. The > > > proper way to do this is something like: > > > > class User < ActiveRecord::Base > > > has_many :admirations > > > has_many :admirers, :through => :admirations > > > has_many :admirees, :through => :admirations > > > end > > > > class Admiration < ActiveRecord::Base > > > belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" > > > belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" > > > end > > > > I think that should work (too late to plug it in and test it)... > > > > The advantage of HM:T is that you can add extra data to the > > > relationship. For example, you could put "Admired since XX-XX-XXXX" > > > and plugin the created_at attribute on the Admiration, or you could > > > add in a field like "knows_admiree" and put something like "Knows and > > > admires (x)." > > > > --Jeremy > > > > On Dec 22, 2007 4:20 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Can one user admire many other users? Perhaps have another table outside of > > > > users called admirees_admirers and store the admiree_id and admirer_id in > > > > there. > > > > > Reference via the user model like this: > > > > > class User < ActiveRecord::Base > > > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > > > :foreign_key => "admiree_id" > > > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > > > :foreign_key => "admirer_id" > > > > end > > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Im having trouble developing the database structure for the following > > > > > scenario. I have a table that only contains 2 fields for each > > > > > object. It has a admirer_id and an admired_id. There is also a user > > > > > table. Now all I want to do is go through the admirers table and for > > > > > each admired_id, take the corresponding admirer_id and collect all the > > > > > users that have a corresponding user_id to this admirer_id. Im having > > > > > trouble setting up the database relationships and how I should go > > > > > about this in general. I think this may involve using a self- > > > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > > > greatly appreciated. Thanks! ;) > > > > >http://www.frozenplague.net > > > > --http://www.jeremymcanally.com/ > > > > My books: > > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-25 22:46 UTC
Re: please someone help with this active record question! its driving me crazy
Hmm. Again, not trying this in a console or anything, but off the top of my head, you could try: @avatars = @user.admireds.map {|user| user.avatar} Not sure that would work, but you should be able to use some of the Enumerable methods like that to make it happen. --Jeremy On Dec 25, 2007 3:51 PM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Perfect! This works, you are the man. Thank you. I can now collect > all the user_id''s of the admirers of a user by using @user.admireds. > Now if I can only trouble you for some more brain power, I am trying > to use these user_id''s to create an instance of @avatars. There is a > user_id field in each avatar object and I want to gather all the > avatar objects in @avatars that correspond to to the @user.admireds. > I think this may be simple, but it is still giving me trouble. Any > thoughts?? Thanks! > > On Dec 25, 8:28 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Looks like maybe that was my fault...try this. :) > > > > class User < ActiveRecord::Base > > has_many :admirations, :foreign_key => ''admirer_id'', :class_name => > > ''Admiration'' > > has_many :admirees, :through => :admirations > > > > # Bad association name but you can name it whatever > > has_many :admireds, :foreign_key => ''admiree_id'', :class_name => ''Admiration'' > > has_many :admirers, :through => :admireds > > end > > > > class Admiration < ActiveRecord::Base > > belongs_to :admirer, :class_name => "User" > > belongs_to :admiree, :class_name => "User" > > end > > > > I haven''t tried this either but now that my brain is more awake, I > > think this should work. :) > > > > --Jeremy > > > > > On Dec 25, 2007 10:03 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > Thanks, it looks like Im getting closer, but I am still getting an > > > error: > > > Mysql::Error: Unknown column ''admirations.user_id'' in ''where clause'': > > > SELECT users.* FROM users INNER JOIN admirations ON users.id > > > admirations.admirer_id WHERE ((admirations.user_id = 1)) > > > > > On Dec 25, 4:12 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > HABTM is pretty much deprecated compared to has_many :through. The > > > > proper way to do this is something like: > > > > > > class User < ActiveRecord::Base > > > > has_many :admirations > > > > has_many :admirers, :through => :admirations > > > > has_many :admirees, :through => :admirations > > > > end > > > > > > class Admiration < ActiveRecord::Base > > > > belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" > > > > belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" > > > > end > > > > > > I think that should work (too late to plug it in and test it)... > > > > > > The advantage of HM:T is that you can add extra data to the > > > > relationship. For example, you could put "Admired since XX-XX-XXXX" > > > > and plugin the created_at attribute on the Admiration, or you could > > > > add in a field like "knows_admiree" and put something like "Knows and > > > > admires (x)." > > > > > > --Jeremy > > > > > > On Dec 22, 2007 4:20 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Can one user admire many other users? Perhaps have another table outside of > > > > > users called admirees_admirers and store the admiree_id and admirer_id in > > > > > there. > > > > > > > Reference via the user model like this: > > > > > > > class User < ActiveRecord::Base > > > > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > > > > :foreign_key => "admiree_id" > > > > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > > > > :foreign_key => "admirer_id" > > > > > end > > > > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > Im having trouble developing the database structure for the following > > > > > > scenario. I have a table that only contains 2 fields for each > > > > > > object. It has a admirer_id and an admired_id. There is also a user > > > > > > table. Now all I want to do is go through the admirers table and for > > > > > > each admired_id, take the corresponding admirer_id and collect all the > > > > > > users that have a corresponding user_id to this admirer_id. Im having > > > > > > trouble setting up the database relationships and how I should go > > > > > > about this in general. I think this may involve using a self- > > > > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > > > > greatly appreciated. Thanks! ;) > > > > > >http://www.frozenplague.net > > > > > > --http://www.jeremymcanally.com/ > > > > > > My books: > > > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > > > --http://www.jeremymcanally.com/ > > > > My books: > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2007-Dec-25 23:16 UTC
Re: please someone help with this active record question! its driving me crazy
HABTM does have its use. It is NOT deprecated and will NEVER be removed out of Rails, period. If your domain does not need any attributes other than the foreign keys of the tables that are connected then HABTM is good enough. In this case the association class is just a join table. When your application demands that other attributes needs to be persisted that belongs to the association class then you have to go for the has_many : through relationship. On Dec 25, 2007 2:46 PM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hmm. Again, not trying this in a console or anything, but off the top > of my head, you could try: > >-- http://www.rubyplus.org/ Free Ruby and Rails Screencasts --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-25 23:25 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 25, 3:16 pm, "Bala Paranj" <bcpar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > When your application demands that other attributes needs to be persisted > that belongs to the association class then you have to go for the has_many : > through relationship.I also had a weird case with a many-to-many relationship between two classes in the same inheritance hierarchy. I had to use hmt for that. ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-25 23:56 UTC
Re: please someone help with this active record question! its driving me crazy
I never said it was going to be removed from Rails. I do think it''s a gimped solution compared to HMT and is generally viewed as, shall we say, outmoded in 99% of cases. --Jeremy On Dec 25, 2007 6:16 PM, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> HABTM does have its use. It is NOT deprecated and will NEVER be removed out > of Rails, period. If your domain does not need any attributes other than > the foreign keys of the tables that are connected then HABTM is good enough. > In this case the association class is just a join table. > When your application demands that other attributes needs to be persisted > that belongs to the association class then you have to go for the has_many > :through relationship. > > > > On Dec 25, 2007 2:46 PM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hmm. Again, not trying this in a console or anything, but off the top > > of my head, you could try: > > > > > > > > > > > -- > http://www.rubyplus.org/ > Free Ruby and Rails Screencasts > > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2007-Dec-26 00:32 UTC
Re: please someone help with this active record question! its driving me crazy
Quoting Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hmm. Again, not trying this in a console or anything, but off the top > of my head, you could try: > > @avatars = @user.admireds.map {|user| user.avatar} >If this works, a more compact form that may work is: @avatars = @user.admireds.map(&:avatar) HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bcp
2007-Dec-26 05:21 UTC
Re: please someone help with this active record question! its driving me crazy
It is not out moded. Those two constructs are used in different scenarios. Sent from my iPhone On Dec 25, 2007, at 3:56 PM, "Jeremy McAnally" <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I never said it was going to be removed from Rails. I do think it''s a > gimped solution compared to HMT and is generally viewed as, shall we > say, outmoded in 99% of cases. > > --Jeremy > > On Dec 25, 2007 6:16 PM, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> HABTM does have its use. It is NOT deprecated and will NEVER be >> removed out >> of Rails, period. If your domain does not need any attributes >> other than >> the foreign keys of the tables that are connected then HABTM is >> good enough. >> In this case the association class is just a join table. >> When your application demands that other attributes needs to be >> persisted >> that belongs to the association class then you have to go for the >> has_many >> :through relationship. >> >> >> >> On Dec 25, 2007 2:46 PM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >>> >>> Hmm. Again, not trying this in a console or anything, but off the >>> top >>> of my head, you could try: >>> >>> >>> >>> >> >> >> -- >> http://www.rubyplus.org/ >> Free Ruby and Rails Screencasts >> >> >>> >> > > > > -- > http://www.jeremymcanally.com/ > > My books: > Ruby in Practice > http://www.manning.com/mcanally/ > > My free Ruby e-book > http://www.humblelittlerubybook.com/ > > My blogs: > http://www.mrneighborly.com/ > http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2007-Dec-26 18:17 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 25, 2007 6:56 PM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I never said it was going to be removed from Rails. I do think it''s a > gimped solution compared to HMT and is generally viewed as, shall we > say, outmoded in 99% of cases.I think that 99% is way overstated. HABTM works just fine for cases where there are no attributes needed in the association. In my experience, that''s probably 90% of the time. What''s deprecated is push_with_attributes, NOT HABTM. If you need what Josh calls a "rich association" then, yes, HMT is the way to model it. In fact HABTM got a little love in Rails 2.0 with the new fixture support which was included from the rathole plugin http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier You can now use symbolic names for HABTM attributes and the join table will automatically be populated. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-26 19:06 UTC
Re: please someone help with this active record question! its driving me crazy
I can honestly say that in 99% of cases where I _would_ use HABTM, I use HM:T because I can almost guarantee that I will be attaching extra data to the relationship OR I will want to fetch all the relationships separately (for example, I want to see all book selections for a bookstore or something). Perhaps it''s the work that I''ve been doing and have always done, but that''s the case that I''ve found myself in. --Jeremy On Dec 26, 2007 1:17 PM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Dec 25, 2007 6:56 PM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I never said it was going to be removed from Rails. I do think it''s a > > gimped solution compared to HMT and is generally viewed as, shall we > > say, outmoded in 99% of cases. > > I think that 99% is way overstated. > > HABTM works just fine for cases where there are no attributes needed > in the association. In my experience, that''s probably 90% of the > time. > > What''s deprecated is push_with_attributes, NOT HABTM. If you need > what Josh calls a "rich association" then, yes, HMT is the way to > model it. > > In fact HABTM got a little love in Rails 2.0 with the new fixture > support which was included from the rathole plugin > > http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier > > You can now use symbolic names for HABTM attributes and the join table > will automatically be populated. > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2007-Dec-26 20:52 UTC
Re: please someone help with this active record question! its driving me crazy
Please don''t confuse the newbies with your opinions. It is your wish to follow a speculative design in your projects. Newbies need guidelines to follow for a given situation. On Dec 26, 2007 11:06 AM, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I can honestly say that in 99% of cases where I _would_ use HABTM, I > use HM: > > Perhaps it''s the work that I''ve been doing and have always done, but > that''s the case that I''ve found myself in. > > --Jeremy > >-- http://www.rubyplus.org/ Free Ruby and Rails Screencasts --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-27 01:13 UTC
Re: please someone help with this active record question! its driving me crazy
This has basically degenerated into a stupid pissing match. This is all opinion. You could easily do the same thing we''re talking about here with raw MySQL drivers and regular Ruby classes, but AR and habtm or hm:t is a better solution. I was merely sharing what I''ve learned to be best practice and what has been deemed less than best practice by myself and others. As for "speculative design," it''s not "speculative" if one is simply thinking ahead. Going about software without doing that is what leads to "refactoring" that are actually just fixing your stupid mistakes because you didn''t think when you were writing code. If you think that implementing something that I can nearly always expect to use is speculative, one could say that any abstraction is speculative. "I''ll probably need to share this piece of ERb so I''ll push it into a partial" or "I''ll probably be adding a lot of different types of data to this collection so it might be better to make it a Hash rather than an Array so I can get at it by key" are both, by your definition, statements of speculative design but are sound design decisions to make. Surely you can''t contend that thinking ahead isn''t a good idea? As for this thread, I think we''re done here. My HM:T code worked for him, a newbie has been helped, problem solved. --Jeremy On Dec 26, 2007 3:52 PM, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Please don''t confuse the newbies with your opinions. It is your wish to > follow a speculative design in your projects. Newbies need guidelines to > follow for a given situation. > > > On Dec 26, 2007 11:06 AM, Jeremy McAnally < jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I can honestly say that in 99% of cases where I _would_ use HABTM, I > > use HM: > > > > > > Perhaps it''s the work that I''ve been doing and have always done, but > > that''s the case that I''ve found myself in. > > > > --Jeremy > > > > > > > -- > > http://www.rubyplus.org/ > Free Ruby and Rails Screencasts > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-27 15:10 UTC
Re: please someone help with this active record question! its driving me crazy
Is the .map method kinda like a for loop? On Dec 25, 2:46 pm, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmm. Again, not trying this in a console or anything, but off the top > of my head, you could try: > > @avatars = @user.admireds.map {|user| user.avatar} > > Not sure that would work, but you should be able to use some of the > Enumerable methods like that to make it happen. > > --Jeremy > > On Dec 25, 2007 3:51 PM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Perfect! This works, you are the man. Thank you. I can now collect > > all the user_id''s of the admirers of a user by using @user.admireds. > > Now if I can only trouble you for some more brain power, I am trying > > to use these user_id''s to create an instance of @avatars. There is a > > user_id field in each avatar object and I want to gather all the > > avatar objects in @avatars that correspond to to the @user.admireds. > > I think this may be simple, but it is still giving me trouble. Any > > thoughts?? Thanks! > > > On Dec 25, 8:28 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > Looks like maybe that was my fault...try this. :) > > > > class User < ActiveRecord::Base > > > has_many :admirations, :foreign_key => ''admirer_id'', :class_name => > > > ''Admiration'' > > > has_many :admirees, :through => :admirations > > > > # Bad association name but you can name it whatever > > > has_many :admireds, :foreign_key => ''admiree_id'', :class_name => ''Admiration'' > > > has_many :admirers, :through => :admireds > > > end > > > > class Admiration < ActiveRecord::Base > > > belongs_to :admirer, :class_name => "User" > > > belongs_to :admiree, :class_name => "User" > > > end > > > > I haven''t tried this either but now that my brain is more awake, I > > > think this should work. :) > > > > --Jeremy > > > > On Dec 25, 2007 10:03 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Thanks, it looks like Im getting closer, but I am still getting an > > > > error: > > > > Mysql::Error: Unknown column ''admirations.user_id'' in ''where clause'': > > > > SELECT users.* FROM users INNER JOIN admirations ON users.id > > > > admirations.admirer_id WHERE ((admirations.user_id = 1)) > > > > > On Dec 25, 4:12 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > > wrote: > > > > > HABTM is pretty much deprecated compared to has_many :through. The > > > > > proper way to do this is something like: > > > > > > class User < ActiveRecord::Base > > > > > has_many :admirations > > > > > has_many :admirers, :through => :admirations > > > > > has_many :admirees, :through => :admirations > > > > > end > > > > > > class Admiration < ActiveRecord::Base > > > > > belongs_to :admirer, :class_name => "User", :foreign_key => "admirer_id" > > > > > belongs_to :admiree, :class_name => "User", :foreign_key => "admiree_id" > > > > > end > > > > > > I think that should work (too late to plug it in and test it)... > > > > > > The advantage of HM:T is that you can add extra data to the > > > > > relationship. For example, you could put "Admired since XX-XX-XXXX" > > > > > and plugin the created_at attribute on the Admiration, or you could > > > > > add in a field like "knows_admiree" and put something like "Knows and > > > > > admires (x)." > > > > > > --Jeremy > > > > > > On Dec 22, 2007 4:20 AM, Ryan Bigg <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Can one user admire many other users? Perhaps have another table outside of > > > > > > users called admirees_admirers and store the admiree_id and admirer_id in > > > > > > there. > > > > > > > Reference via the user model like this: > > > > > > > class User < ActiveRecord::Base > > > > > > has_and_belongs_to_many :admirers, :join_table => "admirees_admirers", > > > > > > :foreign_key => "admiree_id" > > > > > > has_and_belongs_to_many :admirees, :join_table => "admirees_admirers", > > > > > > :foreign_key => "admirer_id" > > > > > > end > > > > > > > On Dec 22, 2007 5:00 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > Im having trouble developing the database structure for the following > > > > > > > scenario. I have a table that only contains 2 fields for each > > > > > > > object. It has a admirer_id and an admired_id. There is also a user > > > > > > > table. Now all I want to do is go through the admirers table and for > > > > > > > each admired_id, take the corresponding admirer_id and collect all the > > > > > > > users that have a corresponding user_id to this admirer_id. Im having > > > > > > > trouble setting up the database relationships and how I should go > > > > > > > about this in general. I think this may involve using a self- > > > > > > > referential has_and_belongs_to_many relationship? Any help/advice is > > > > > > > greatly appreciated. Thanks! ;) > > > > > > >http://www.frozenplague.net > > > > > > --http://www.jeremymcanally.com/ > > > > > > My books: > > > > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > > > --http://www.jeremymcanally.com/ > > > > My books: > > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor
2007-Dec-27 15:41 UTC
Re: please someone help with this active record question! its driving me crazy
Quoting Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Is the .map method kinda like a for loop? >Yes, and more. It iterates through all elements in the object and returns a new array with the return value of the block.> > @avatars = @user.admireds.map {|user| user.avatar}This example sets @avatars to the value of avatar for every element in @user.admireds. For a block as simple as this, the following will probably do the same thing. @avatars = @user.admireds.map(&:avatar) I saw this usage on a Web page somewhere and use it regularly, but I don''t know Ruby well enough to understand exactly what it means. HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-27 15:47 UTC
Re: please someone help with this active record question! its driving me crazy
On 27 Dec 2007, at 15:41, Jeffrey L. Taylor wrote:> > > @avatars = @user.admireds.map(&:avatar) > > I saw this usage on a Web page somewhere and use it regularly, but I > don''t > know Ruby well enough to understand exactly what it means. >It''s a shortcut. The & tells ruby that the parameter being passed should be used as the block to this method. ruby calls to_proc on it (in this case :avatar) to make sure it gets a proc. Symbol#to_proc creates a proc which when given an object calls the named method on it Fred> HTH, > Jeffrey > > >--~--~---------~--~----~------------~-------~--~----~ 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-27 16:53 UTC
Re: please someone help with this active record question! its driving me crazy
I think this code is calling the .avatar for each user.admireds object though. And that method is undefined for that class. I dont think this works. What about doing something like: @avatars Avatar.find(:all).select {|avatar| avatar.user_id =@user.admireds } This doesnt work though b/c it doesnt iterate through the @user.admireds. This is the problem, I need to do two iterations and make comparisons with each. I''m just not sure how to do this. On Dec 27, 7:41 am, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> wrote:> Quoting Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > Is the .map method kinda like a for loop? > > Yes, and more. It iterates through all elements in the object and returns a > new array with the return value of the block. > > > > @avatars = @user.admireds.map {|user| user.avatar} > > This example sets @avatars to the value of avatar for every element in > @user.admireds. For a block as simple as this, the following will probably > do the same thing. > > @avatars = @user.admireds.map(&:avatar) > > I saw this usage on a Web page somewhere and use it regularly, but I don''t > know Ruby well enough to understand exactly what it means. > > HTH, > Jeffrey--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-27 18:31 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 27, 7:10 am, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is the .map method kinda like a for loop?I usually use its synonym - collect. To me, that better describes the output. But that''s just my preference. ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-27 18:36 UTC
Re: please someone help with this active record question! its driving me crazy
Oh, okay. You could do this then (I think): @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)} Why not make Avatar belong to a User (you already have user_id and would just have to put belongs_to :user in Avatar and has_one :avatar in User)? --Jeremy On Dec 27, 2007 11:53 AM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I think this code is calling the .avatar for each user.admireds object > though. And that method is undefined for that class. I dont think > this works. What about doing something like: @avatars > Avatar.find(:all).select {|avatar| avatar.user_id => @user.admireds } > This doesnt work though b/c it doesnt iterate through the > @user.admireds. This is the problem, I need to do two iterations and > make comparisons with each. I''m just not sure how to do this. > > On Dec 27, 7:41 am, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> > wrote: > > > Quoting Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > > > Is the .map method kinda like a for loop? > > > > Yes, and more. It iterates through all elements in the object and returns a > > new array with the return value of the block. > > > > > > @avatars = @user.admireds.map {|user| user.avatar} > > > > This example sets @avatars to the value of avatar for every element in > > @user.admireds. For a block as simple as this, the following will probably > > do the same thing. > > > > @avatars = @user.admireds.map(&:avatar) > > > > I saw this usage on a Web page somewhere and use it regularly, but I don''t > > know Ruby well enough to understand exactly what it means. > > > > HTH, > > Jeffrey > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-27 19:33 UTC
Re: please someone help with this active record question! its driving me crazy
Right. I have already defined this relationship but when I do @user.admireds.avatar it says: undefined method `avatar'' for Admiration:Class. so I dont think @user.admireds is creating user objects in the first place. On Dec 27, 10:36 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, okay. You could do this then (I think): > > @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)} > > Why not make Avatar belong to a User (you already have user_id and > would just have to put belongs_to :user in Avatar and has_one :avatar > in User)? > > --Jeremy > > On Dec 27, 2007 11:53 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I think this code is calling the .avatar for each user.admireds object > > though. And that method is undefined for that class. I dont think > > this works. What about doing something like: @avatars > > Avatar.find(:all).select {|avatar| avatar.user_id => > @user.admireds } > > This doesnt work though b/c it doesnt iterate through the > > @user.admireds. This is the problem, I need to do two iterations and > > make comparisons with each. I''m just not sure how to do this. > > > On Dec 27, 7:41 am, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> > > wrote: > > > > Quoting Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > Is the .map method kinda like a for loop? > > > > Yes, and more. It iterates through all elements in the object and returns a > > > new array with the return value of the block. > > > > > > @avatars = @user.admireds.map {|user| user.avatar} > > > > This example sets @avatars to the value of avatar for every element in > > > @user.admireds. For a block as simple as this, the following will probably > > > do the same thing. > > > > @avatars = @user.admireds.map(&:avatar) > > > > I saw this usage on a Web page somewhere and use it regularly, but I don''t > > > know Ruby well enough to understand exactly what it means. > > > > HTH, > > > Jeffrey > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2007-Dec-27 21:21 UTC
Re: please someone help with this active record question! its driving me crazy
Oh, fool am I! You''re right. You''d need to do something like: @avatars = @user.admireds.map{|adm| adm.admirer.avatar } Sorry about that! You might need to change `admirer` to `admiree` depending on which side of the relationship you''re doing (sorry about not being able to scrollback and see but I''m in a bit of a hurry :)). --Jeremy On Dec 27, 2007 2:33 PM, Dave Lynam <dlynam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Right. I have already defined this relationship but when I do > @user.admireds.avatar it says: undefined method `avatar'' for > Admiration:Class. > so I dont think @user.admireds is creating user objects in the first > place. > > On Dec 27, 10:36 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Oh, okay. You could do this then (I think): > > > > @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)} > > > > Why not make Avatar belong to a User (you already have user_id and > > would just have to put belongs_to :user in Avatar and has_one :avatar > > in User)? > > > > --Jeremy > > > > On Dec 27, 2007 11:53 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > > I think this code is calling the .avatar for each user.admireds object > > > though. And that method is undefined for that class. I dont think > > > this works. What about doing something like: @avatars > > > Avatar.find(:all).select {|avatar| avatar.user_id => > > @user.admireds } > > > This doesnt work though b/c it doesnt iterate through the > > > @user.admireds. This is the problem, I need to do two iterations and > > > make comparisons with each. I''m just not sure how to do this. > > > > > On Dec 27, 7:41 am, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> > > > wrote: > > > > > > Quoting Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > Is the .map method kinda like a for loop? > > > > > > Yes, and more. It iterates through all elements in the object and returns a > > > > new array with the return value of the block. > > > > > > > > @avatars = @user.admireds.map {|user| user.avatar} > > > > > > This example sets @avatars to the value of avatar for every element in > > > > @user.admireds. For a block as simple as this, the following will probably > > > > do the same thing. > > > > > > @avatars = @user.admireds.map(&:avatar) > > > > > > I saw this usage on a Web page somewhere and use it regularly, but I don''t > > > > know Ruby well enough to understand exactly what it means. > > > > > > HTH, > > > > Jeffrey > > > > --http://www.jeremymcanally.com/ > > > > My books: > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Dave Lynam
2007-Dec-28 06:34 UTC
Re: please someone help with this active record question! its driving me crazy
This gives me a null object. Cause its basically saying @user.admireds.admirer.avatar...which creates a null object I guess. Im pretty confused now, Ive been tinkering with this for a while now. Any other ideas are appreciated. Thanks. On Dec 27, 1:21 pm, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, fool am I! You''re right. > > You''d need to do something like: > > @avatars = @user.admireds.map{|adm| adm.admirer.avatar } > > Sorry about that! You might need to change `admirer` to `admiree` > depending on which side of the relationship you''re doing (sorry about > not being able to scrollback and see but I''m in a bit of a hurry :)). > > --Jeremy > > On Dec 27, 2007 2:33 PM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Right. I have already defined this relationship but when I do > > @user.admireds.avatar it says: undefined method `avatar'' for > > Admiration:Class. > > so I dont think @user.admireds is creating user objects in the first > > place. > > > On Dec 27, 10:36 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > Oh, okay. You could do this then (I think): > > > > @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)} > > > > Why not make Avatar belong to a User (you already have user_id and > > > would just have to put belongs_to :user in Avatar and has_one :avatar > > > in User)? > > > > --Jeremy > > > > On Dec 27, 2007 11:53 AM, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I think this code is calling the .avatar for each user.admireds object > > > > though. And that method is undefined for that class. I dont think > > > > this works. What about doing something like: @avatars > > > > Avatar.find(:all).select {|avatar| avatar.user_id => > > > @user.admireds } > > > > This doesnt work though b/c it doesnt iterate through the > > > > @user.admireds. This is the problem, I need to do two iterations and > > > > make comparisons with each. I''m just not sure how to do this. > > > > > On Dec 27, 7:41 am, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> > > > > wrote: > > > > > > Quoting Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > Is the .map method kinda like a for loop? > > > > > > Yes, and more. It iterates through all elements in the object and returns a > > > > > new array with the return value of the block. > > > > > > > > @avatars = @user.admireds.map {|user| user.avatar} > > > > > > This example sets @avatars to the value of avatar for every element in > > > > > @user.admireds. For a block as simple as this, the following will probably > > > > > do the same thing. > > > > > > @avatars = @user.admireds.map(&:avatar) > > > > > > I saw this usage on a Web page somewhere and use it regularly, but I don''t > > > > > know Ruby well enough to understand exactly what it means. > > > > > > HTH, > > > > > Jeffrey > > > > --http://www.jeremymcanally.com/ > > > > My books: > > > Ruby in Practicehttp://www.manning.com/mcanally/ > > > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.com/ > > --http://www.jeremymcanally.com/ > > My books: > Ruby in Practicehttp://www.manning.com/mcanally/ > > My free Ruby e-bookhttp://www.humblelittlerubybook.com/ > > My blogs:http://www.mrneighborly.com/http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Thufir
2008-Jan-18 07:56 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 27 2007, 10:36 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, okay. You could do this then (I think): > > @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)}[...] Would you explain this a bit? @avatars is an object. Is it a collection of users? thanks, Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thufir
2008-Jan-18 08:03 UTC
Re: please someone help with this active record question! its driving me crazy
On Dec 26 2007, 10:17 am, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...]> What''s deprecated ispush_with_attributes, NOT HABTM. If you need > what Josh calls a "rich association" then, yes, HMT is the way to > model it. > > In fact HABTM got a little love in Rails 2.0 with the new fixture > support which was included from the rathole plugin > > http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fi... > > You can now use symbolic names for HABTM attributes and the join table > will automatically be populated.[...] Am I alone in wondering what replaces push_with_attributes? This is in regards to has_many through. thanks, Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2008-Jan-18 09:26 UTC
Re: please someone help with this active record question! it
okay so, ################################## class User < ActiveRecord::Base has_many :admirations, :foreign_key => ''admirer_id'', :class_name => ''Admiration'' has_many :admirees, :through => :admirations has_many :admireds, :foreign_key => ''admiree_id'', :class_name => ''Admiration'' has_many :admirers, :through => :admireds has_one :avatar end ====================================================================== class Admiration < ActiveRecord::Base belongs_to :admirer, :class_name => "User" belongs_to :admiree, :class_name => "User" end ======================================================================= class Avatar < ActiveRecord::Base belongs_to :user end ############################################## Is this right so far? user = User.find(1) user.admirers #=> Array of User Objects user.admirees #=> Array of User Objects user.admirers.map{|a| a.avatar} #=> Array of Avatar Objects user.avatar #=> Avatar Object anything I''ve missed? -- 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 -~----------~----~----~----~------~----~------~--~---
Russell Norris
2008-Jan-18 15:25 UTC
Re: please someone help with this active record question! its driving me crazy
has_many :through replaced it. If you need attributes on yr join model, has_many :through is the way to do it. has_many :through doesn''t need push_with_attributes because it has an ActiveRecord model with proper attributes. You can just create an instance of that model and set its attributes as needed. On Jan 18, 2008 3:03 AM, Thufir <hawat.thufir-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Am I alone in wondering what replaces push_with_attributes? This is > in regards to has_many through. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy McAnally
2008-Jan-18 16:04 UTC
Re: please someone help with this active record question! its driving me crazy
No, @user.admireds is a collection of User objects. When you do #map, it iterates the collection and replaces the current index with the return value of the block. So in this case, an array of User objects is transformed into an array of avatars for those users. So, @avatars becomes an array of Avatar objects. Read up on the #map method and it will make more sense. :) --Jeremy On Jan 18, 2008 2:56 AM, Thufir <hawat.thufir-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Dec 27 2007, 10:36 am, "Jeremy McAnally" <jeremymcana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > Oh, okay. You could do this then (I think): > > > > @avatars = @user.admireds.map{|adm| Avatar.find_by_user_id(adm.id)} > [...] > > Would you explain this a bit? > > @avatars is an object. > > Is it a collection of users? > > > > thanks, > > Thufir > > > >-- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
Thufir
2008-Jan-18 18:17 UTC
Re: please someone help with this active record question! its driving me crazy
On Fri, 18 Jan 2008 10:25:36 -0500, Russell Norris wrote:> has_many :through replaced it. If you need attributes on yr join model, > has_many :through is the way to do it. has_many :through doesn''t need > push_with_attributes because it has an ActiveRecord model with proper > attributes. You can just create an instance of that model and set its > attributes as needed.Yes, I''m realizing this :) How do I go about creating an instance of the model, please? thufir@arrakis ~/strawr $ thufir@arrakis ~/strawr $ cat -n app/controllers/feeds_controller.rb | tail -n 47 | head -n 14 68 69 70 def add_tag 71 72 @feed = Feed.find(params[:id]) 73 @feed.tags.push_with_attributes( 74 Tag.find(params[:tag][:id])) 75 76 if @feed.save 77 flash[:notice] = ''Tag has been added!'' 78 end 79 redirect_to :action => ''show'', :id => params[:id] 80 end 81 thufir@arrakis ~/strawr $ above code is at: http://strawr.googlecode.com/svn/trunk/ thanks, Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thufir
2008-Jan-18 18:20 UTC
Re: please someone help with this active record question! its driving me crazy
On Fri, 18 Jan 2008 11:04:17 -0500, Jeremy McAnally wrote:> When you do #map, it iterates the collection and replaces the current > index with the return value of the block. So in this case, an array of > User objects is transformed into an array of avatars for those users. > So, @avatars becomes an array of Avatar objects. > > Read up on the #map method and it will make more sense.I''m going have to RTFM! :) It looks kinda perlish, but not quite as arcane. -Thufir --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2008-Jan-18 18:49 UTC
Re: please someone help with this active record question! its driving me crazy
On Jan 18, 10:20 am, Thufir <hawat.thu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Read up on the #map method and it will make more sense. > > I''m going have to RTFM! :) > It looks kinda perlish, but not quite as arcane.It''s actually kinda Smalltalkish, if that makes it any better. :) ///ark --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---