Hi, I have two tables, "users" and "users_emails". users have the following columns: |id|name| users_emails have the following columns: |user_id|email| The classes look like the following: class UsersEmail< ActiveRecord::Base belongs_to :users end class User< ActiveRecord::Base has_many :users_emails end --- What I''m trying to do is find all the users whom have a particular email. I can do it has the following: 1) get all user_id''s from user_emails table who has email = :email (user form input) 2) then for those user_ids get the user ojects from the users table by :find(user_id) I can get the email''s based on user name by the following: User.find(1).user_emails but this is not what I want. I want Users whom have a particular email address. I''m wondering what is the nice way of doing this (the rails way using methods on ActiveRecord). Or perhaps my assoications and tables are not correct to facilitate such a query. The following does not work: emails = UsersEmail.find(:condition =>...) emails.users Any help would be greatly appreciated. -- 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 -~----------~----~----~----~------~----~------~--~---
Try this: UserMail.find_by_email("foo-+RB1Aph5k6s@public.gmane.org").collect(&:user) It returns an array with all users with email "foo-+RB1Aph5k6s@public.gmane.org". Do you want this? On Aug 22, 5:58 am, Mike Jowee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > I have two tables, "users" and "users_emails". > > users have the following columns: |id|name| > users_emails have the following columns: |user_id|email| > > The classes look like the following: > class UsersEmail< ActiveRecord::Base > belongs_to :users > end > > class User< ActiveRecord::Base > has_many :users_emails > end > > --- > > What I''m trying to do is find all the users whom have a particular > email. > I can do it has the following: > 1) get all user_id''s from user_emails table who has email = :email (user > form input) > 2) then for those user_ids get the user ojects from the users table by > :find(user_id) > > I can get the email''s based on user name by the following: > User.find(1).user_emails but this is not what I want. I want > Users whom have a particular email address. > > I''m wondering what is the nice way of doing this (the rails way using > methods on ActiveRecord). Or perhaps my assoications and tables are not > correct to facilitate such a query. The following does not work: > emails = UsersEmail.find(:condition =>...) > emails.users > > Any help would be greatly appreciated. > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Hi, There are quite a few inconsistencies in your code, not sure if they are typos in the post or actual errors in your code. You should have class UsersEmail < AR::Base belongs_to :user # note the singular here! end Then you say you have a user_emails table: If your class is UsersEmail, your table should be users_emails (users is plural). I think class UserEmail would be more appropriate naming, but that''s up to you. David''s code is then most elegant to fetch all users with a particular e-mail address. To address why your own code is not working: emails = UsersEmail.find(:conditions => ...) will return you an Array of UsersEmail objects. So if you want to construct an Array of User objects from that, you need to indeed to apply a .collect or .map to it: foundusers = emails.map { |e| e.user } # note the singular user due to the belongs_to clause Again, instead of using :conditions, you can use a dynamic finder if all you need is find on a specific column''s content: UsersEmail.find_by_email("...") Dirk. On 22 aug, 05:58, Mike Jowee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > I have two tables, "users" and "users_emails". > > users have the following columns: |id|name| > users_emails have the following columns: |user_id|email| > > The classes look like the following: > class UsersEmail< ActiveRecord::Base > belongs_to :users > end > > class User< ActiveRecord::Base > has_many :users_emails > end > > --- > > What I''m trying to do is find all the users whom have a particular > email. > I can do it has the following: > 1) get all user_id''s from user_emails table who has email = :email (user > form input) > 2) then for those user_ids get the user ojects from the users table by > :find(user_id) > > I can get the email''s based on user name by the following: > User.find(1).user_emails but this is not what I want. I want > Users whom have a particular email address. > > I''m wondering what is the nice way of doing this (the rails way using > methods on ActiveRecord). Or perhaps my assoications and tables are not > correct to facilitate such a query. The following does not work: > emails = UsersEmail.find(:condition =>...) > emails.users > > Any help would be greatly appreciated. > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Hi, Thanks for the quick respones. David''s answer works great. I''m still new to Rails, I was wondering what ":&user" does? I know the collect method applies ":&user" to all the elements in the hash. Thanks again. deegee wrote:> Hi, > There are quite a few inconsistencies in your code, not sure if they > are typos in the post or actual errors in your code. You should have > class UsersEmail < AR::Base > belongs_to :user # note the singular here! > end > > Then you say you have a user_emails table: If your class is > UsersEmail, your table should be users_emails (users is plural). I > think class UserEmail would be more appropriate naming, but that''s up > to you. > > David''s code is then most elegant to fetch all users with a particular > e-mail address. To address why your own code is not working: > emails = UsersEmail.find(:conditions => ...) will return you an Array > of UsersEmail objects. So if you want to construct an Array of User > objects from that, you need to indeed to apply a .collect or .map to > it: > > foundusers = emails.map { |e| e.user } # note the singular user due to > the belongs_to clause > > Again, instead of using :conditions, you can use a dynamic finder if > all you need is find on a specific column''s content: > UsersEmail.find_by_email("...") > > Dirk.-- 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 -~----------~----~----~----~------~----~------~--~---
It''s a example of ruby sugar code ^^ "UserMail.find_by_email("f...-+RB1Aph5k6s@public.gmane.org").collect(&:user)" and "UserMail.find_by_email("foo-+RB1Aph5k6s@public.gmane.org").collect { |var| var.user }" is the same code. I think it''s useful and it haves some other uses. On 22 ago, 14:15, Mike Jowee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > Thanks for the quick respones. David''s answer works great. > I''m still new to Rails, I was wondering what ":&user" does? I know the > collect method applies ":&user" to all the elements in the hash. > > Thanks again. > > > > > > > > deegee wrote: > > Hi, > > There are quite a few inconsistencies in your code, not sure if they > > are typos in the post or actual errors in your code. You should have > > class UsersEmail < AR::Base > > belongs_to :user # note the singular here! > > end > > > Then you say you have a user_emails table: If your class is > > UsersEmail, your table should be users_emails (users is plural). I > > think class UserEmail would be more appropriate naming, but that''s up > > to you. > > > David''s code is then most elegant to fetch all users with a particular > > e-mail address. To address why your own code is not working: > > emails = UsersEmail.find(:conditions => ...) will return you an Array > > of UsersEmail objects. So if you want to construct an Array of User > > objects from that, you need to indeed to apply a .collect or .map to > > it: > > > foundusers = emails.map { |e| e.user } # note the singular user due to > > the belongs_to clause > > > Again, instead of using :conditions, you can use a dynamic finder if > > all you need is find on a specific column''s content: > > UsersEmail.find_by_email("...") > > > Dirk. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
On 22 Aug 2008, at 13:15, Mike Jowee wrote:> > Hi, > Thanks for the quick respones. David''s answer works great. > I''m still new to Rails, I was wondering what ":&user" does? I know > the > collect method applies ":&user" to all the elements in the hash. >it''s &:user. when you prefix the last argument to a function with & that tells ruby ''this is something like a proc that you should should use as the block for this function). I said like a proc, because ruby doesn''t really care. As long as it has a to_proc method which returns something close enough to a proc then you''re ok. rails adds a to_proc method to symbols that generates an appropriate proc (from 1.8.7/1.9 this is part of the standard library (not 100% sure about 1.8.7) Fred> Thanks again. > > deegee wrote: >> Hi, >> There are quite a few inconsistencies in your code, not sure if they >> are typos in the post or actual errors in your code. You should have >> class UsersEmail < AR::Base >> belongs_to :user # note the singular here! >> end >> >> Then you say you have a user_emails table: If your class is >> UsersEmail, your table should be users_emails (users is plural). I >> think class UserEmail would be more appropriate naming, but that''s up >> to you. >> >> David''s code is then most elegant to fetch all users with a >> particular >> e-mail address. To address why your own code is not working: >> emails = UsersEmail.find(:conditions => ...) will return you an Array >> of UsersEmail objects. So if you want to construct an Array of User >> objects from that, you need to indeed to apply a .collect or .map to >> it: >> >> foundusers = emails.map { |e| e.user } # note the singular user due >> to >> the belongs_to clause >> >> Again, instead of using :conditions, you can use a dynamic finder if >> all you need is find on a specific column''s content: >> UsersEmail.find_by_email("...") >> >> Dirk. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Oh I see. Thanks! david wrote:> It''s a example of ruby sugar code ^^ > > "UserMail.find_by_email("f...-+RB1Aph5k6s@public.gmane.org").collect(&:user)" > and > "UserMail.find_by_email("foo-+RB1Aph5k6s@public.gmane.org").collect { |var| var.user }" > > is the same code. I think it''s useful and it haves some other uses.-- 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 -~----------~----~----~----~------~----~------~--~---