0

我已经实现了用户-组关系模型,其中一个用户可以是一个或多个组的成员和所有者,一个组可以有一个或多个所有者和一个或多个成员。

现在,问题是User.owned_groups即使用户拥有超过 1 个组,也只返回 1 个组。

我面临与User.member_groups,Group.owners和相同的问题Group.members。所有方法都只返回 1 条记录。

以下是数据库中的模型和值。

楷模: -

require 'rubygems'
require 'data_mapper'
require 'dm-types'

module Contacts
  class User
    include DataMapper::Resource

    property :id,         Serial,   :key => true
    property :username,   String,   :required => true, :unique => true

    has n, :group_owners, :child_key => [:owned_group_id]
    has n, :owned_groups, 'Group', :through => :group_owners

    has n, :group_members, :child_key => [:member_group_id]
    has n, :member_groups, 'Group', :through => :group_members

    #------------------------------------------#
    # args should be an array of hashes        #
    # Example: args = [{:id => 1},{:id => 2}]  #
    #------------------------------------------#
    def self.get_users args
      users = Array.new
      args.each do |user|
        all_users = User.all(user)
        all_users.each do |u|
          user_hash = u.attributes

          user_hash[:owned_groups] = Array.new
          u.owned_groups.each do |owned_group|
            g = owned_group.attributes
            user_hash[:owned_groups].push g
          end

          user_hash[:member_groups] = Array.new
          u.member_groups.each do |member_group|
            g = member_group.attributes
            user_hash[:member_groups].push g
          end

          users.push user_hash
        end
      end
      return users
    end
  end

  class Group
    include DataMapper::Resource

    property :id,   Serial, :key => true
    property :name, String, :required => true, :unique => true

    has n, :group_owners, :child_key => [:owner_id]
    has n, :owners, 'User', :through => :group_owners

    has n, :group_members, :child_key => [:member_id]
    has n, :members, 'User', :through => :group_members

    #------------------------------------------#
    # args should be an array of hashes        #
    # Example: args = [{:id => 1},{:id => 2}]  #
    #------------------------------------------#
    def self.get_groups args
      groups = Array.new

      args.each do |group|
        all_groups = Group.all(group)
        all_groups.each do |g|
          group_hash = g.attributes

          group_hash[:owners] = Array.new
          g.owners.each do |owner|
            u = owner.attributes
            group_hash[:owners].push u
          end

          group_hash[:members] = Array.new
          g.members.each do |member|
            u = member.attributes
            group_hash[:members].push u
          end
          groups.push group_hash
        end
      end
      return groups
    end
  end

  class GroupOwner
    include DataMapper::Resource

    property :id, Serial, :key => true
    belongs_to :owner, 'User'
    belongs_to :owned_group, 'Group'
  end

  class GroupMember
    include DataMapper::Resource

    property :id, Serial, :key => true
    belongs_to :member, 'User'
    belongs_to :member_group, 'Group'
  end
end

MySQL:-

mysql> select * from contacts_users;
+----+-----------+
| id | username  |
+----+-----------+
|  1 | testuser1 |
|  2 | testuser2 |
+----+-----------+

mysql> select * from contacts_groups;
+----+------------+
| id | name       |
+----+------------+
|  1 | testgroup1 |
|  2 | testgroup2 |
+----+------------+
2 rows in set (0.00 sec)


mysql> select * from contacts_group_owners;
+----+----------+----------------+
| id | owner_id | owned_group_id |
+----+----------+----------------+
|  1 |        1 |              1 |
|  2 |        2 |              1 |
|  3 |        1 |              2 |
|  4 |        2 |              2 |
+----+----------+----------------+
4 rows in set (0.00 sec)

mysql> select * from contacts_group_members;
+----+-----------+-----------------+
| id | member_id | member_group_id |
+----+-----------+-----------------+
|  1 |         1 |               1 |
|  2 |         2 |               1 |
|  3 |         1 |               2 |
|  4 |         2 |               2 |
+----+-----------+-----------------+
4 rows in set (0.00 sec)

在调试模式下,这是生成的 MySQL 查询User.owned_groups

    SELECT 
      `contacts_groups`.`id`, 
      `contacts_groups`.`name`, 
      `contacts_groups`.`created_at`, 
      `contacts_groups`.`updated_at` 
   FROM `contacts_groups` 
   INNER JOIN `contacts_group_owners` 
      ON `contacts_groups`.`id` = `contacts_group_owners`.`owned_group_id` 
   INNER JOIN `contacts_users` 
      ON `contacts_group_owners`.`owned_group_id` = `contacts_users`.`id` 
   WHERE `contacts_group_owners`.`owned_group_id` = 1 
   GROUP BY 
      `contacts_groups`.`id`, 
      `contacts_groups`.`name`, 
      `contacts_groups`.`created_at`, 
      `contacts_groups`.`updated_at` 
   ORDER BY `contacts_groups`.`id

版本:

红宝石 - 2.0.0-p0

西纳特拉 - 1.3.4

数据映射器 - 1.2.0

我错过了一些微不足道的事情吗?

提前致谢。

4

1 回答 1

0

尝试这个:

   SELECT 
      *
   FROM 
      `contacts_groups` 
   INNER JOIN `contacts_group_owners` 
      ON `contacts_groups`.`id` = `contacts_group_owners`.`owned_group_id` 
   INNER JOIN `contacts_users` 
      ON `contacts_group_owners`.`owner_id` = `contacts_users`.`id` 
   WHERE 
      `contacts_group_owners`.`owned_group_id` = 1 
于 2013-05-13T09:38:30.447 回答