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
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.
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%”