I have tables: users (id, name) computers (id, name) mobiltelefons (id, name) OwnComputers (id, user_id, computer_id) OwnMobiltelefons (id, user_id, mobiltelefon_id) So is it has_many :through or has_and_belongs_to_many or what? I only need to search one "own" table at time: All computers that user "xx" owns OR All Mobiltelefons that user "xx" owns OR who owns mobilitefon "zz" OR who owns computer "yy" So... class Computers < ActiveRecord::Base has_many :OwnComputer has_many :user, :through => :OwnComputer end class OwnComputers < ActiveRecord::Base belongs_to :Computer belongs_to :User end class Users < ActiveRecord::Base ??? end class Mobiltelefons < ActiveRecord::Base ??? end class OwnMobiltelefons < ActiveRecord::Base ??? end -- 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 -~----------~----~----~----~------~----~------~--~---
If I understand correctly what you need, a computer may belong to many users and one user can have many computer, right? same for phones, I assume. In this case, the relationship is :has_and_belongs_to_many, but you do not need the classes OwnComputer and the other one. you just need two tables (without id) which will contain, for each record, a couple of id identifying one coupling (user-computer for one table, user-phone for the other). have a look at: http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many for naming conventions. Regards, Rey9999 On 10 Feb, 15:31, James Bond <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have tables: > > users (id, name) > computers (id, name) > mobiltelefons (id, name) > > OwnComputers (id, user_id, computer_id) > OwnMobiltelefons (id, user_id, mobiltelefon_id) > > So is it has_many :through or has_and_belongs_to_many or what? > > I only need to search one "own" table at time: > All computers that user "xx" owns OR > All Mobiltelefons that user "xx" owns OR > who owns mobilitefon "zz" OR > who owns computer "yy" > > So... > > class Computers < ActiveRecord::Base > has_many :OwnComputer > has_many :user, :through => :OwnComputer > end > > class OwnComputers < ActiveRecord::Base > belongs_to :Computer > belongs_to :User > end > > class Users < ActiveRecord::Base > ??? > end > > class Mobiltelefons < ActiveRecord::Base > ??? > end > > class OwnMobiltelefons < ActiveRecord::Base > ??? > end > -- > 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 -~----------~----~----~----~------~----~------~--~---
Rey9999 wrote:> If I understand correctly what you need, a computer may belong to many > users and one user can have many computer, right? same for phones, I > assume.Yes> In this case, the relationship is :has_and_belongs_to_many, but you doHow can I find all users whose name is "James%" and their computers (not mobiles)? User.find(:all, ??????) -- 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 -~----------~----~----~----~------~----~------~--~---
> How can I find all users whose name is "James%" and their computers (not > mobiles)? > > User.find(:all, ??????)Help ??? -- 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 -~----------~----~----~----~------~----~------~--~---
Hi @users = User.find(:all,:conditions=> [''name LIKE ?'', "James%"]) @user_computers = [] @users.each do |user| @user_computers << user.computers end @user_computers now contains computers you need Sijo -- 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 -~----------~----~----~----~------~----~------~--~---
Sijo Kg wrote: I mean something like this: SELECT users.name, computers.name FROM users, OwnComputers, computers JOIN OwnComputers ON users.id = OwnComputers.users_id JOIN computers ON OwnComputers.computers_id = computers.id WHERE user.name LIKE "James%" -- 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 -~----------~----~----~----~------~----~------~--~---
Or use has_many :through instead. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 11/02/2009, at 11:50 PM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi > > @users = User.find(:all,:conditions=> [''name LIKE ?'', "James%"]) > @user_computers = [] > @users.each do |user| > @user_computers << user.computers > end > > @user_computers now contains computers you need > > > Sijo > -- > 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 by the way if you do that you''ll end up with an array of arrays. Might not be what you want. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 11/02/2009, at 11:50 PM, Sijo Kg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi > > @users = User.find(:all,:conditions=> [''name LIKE ?'', "James%"]) > @user_computers = [] > @users.each do |user| > @user_computers << user.computers > end > > @user_computers now contains computers you need > > > Sijo > -- > 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 -~----------~----~----~----~------~----~------~--~---
Why is a good question here. In other words, what are you going to do with the computers and users once You get them. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 11/02/2009, at 4:42 AM, James Bond <rails-mailing-list@andreas- s.net> wrote:> > Rey9999 wrote: >> If I understand correctly what you need, a computer may belong to >> many >> users and one user can have many computer, right? same for phones, I >> assume. > > Yes > >> In this case, the relationship is :has_and_belongs_to_many, but you >> do > > How can I find all users whose name is "James%" and their computers > (not > mobiles)? > > User.find(:all, ??????) > -- > 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 -~----------~----~----~----~------~----~------~--~---