2016-04-06 9 views
0

私は、グループ内のユーザの統計を追跡するための簡単なRailsアプリケーション(これは私の最初のレールプロジェクトです)を構築する作業をしています。マイグレーションスクリプトを使用してテーブルを作成しましたが、MySqlで見るとすべてのことがきれいに見えますが、他のテーブルにそれらを結合するとすべてのモデルがデータを返すわけではありません。ActiveRecordアソシエーション:私はそれを正しくやっていますか?

私のモデル、マイグレーションスクリプト、またはデータモデルに問題がある人はいますか?ここで

は、私の移行ファイルのスクリプトコードである:ここで

class CreateGroupsUsers < ActiveRecord::Migration 
    def change 

    create_table :types do |t| 
     t.string :name 
    end 

    create_table :groups do |t| 
     t.string :name 
     t.belongs_to :type, index: true 
    end 

    create_table :users do |t| 
     t.string :username 
     t.string :email 
     t.string :first_name 
     t.string :last_name 
     t.string :e_password 
     t.string :salt 
     t.timestamps 
    end 

    create_table :roles do |t| 
     t.string :name 
    end 

    create_table :groups_users, id: false do |t| 
     t.belongs_to :group, index: true 
     t.belongs_to :user, index: true 
     t.belongs_to :role, index: true 
    end 

    create_table :statistics do |t| 
     t.string :name 
    end 

    create_table :groups_users_statistics, id: false do |t| 
     t.string :value 
     t.belongs_to :group, index: true 
     t.belongs_to :user, index: true 
     t.belongs_to :statistic, index: true 
    end 

    end 
end 

は私の私のモデルです:

class Type < ActiveRecord::Base 
    has_many :groups 
end 

class Role < ActiveRecord::Base 
    has_many :groups_users 
end 

class User < ActiveRecord::Base 
    has_and_belongs_to_many :groups 
    has_one :roles, through: :groups_users 
end 

class Group < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    has_one :types 
end 

class Statistic < ActiveRecord::Base 
    //i'm not too sure how to define this model 
end 

、ここでは私のデータモデルは、私は私のモデルを更新

Here's my data model

+0

を正常に動作しているようだ私はあなたが 'にhas_manyを持って – Esse

+1

through'' 'にhas_manyの賛成でhas_and_belongs_to_many'を回避しようとすべきだと思います'グループ'モデルでは間違っていて動作しません。 – Pavan

+0

@Pavan代わりに何をお勧めしますか? – Divide100

答えて

0

ですEsse、Pavan、AndyVのコメントによると、 `type`がモデルにgroups`、あなたが` has_oneの持っている:タイプのすべてが今

class Type < ActiveRecord::Base 
    has_many :groups 
end 

class Role < ActiveRecord::Base 
    has_many :group_users 
end 

class User < ActiveRecord::Base 
    has_many :group_users 
    has_many :groups, through: :group_users 

    has_many :group_user_statistics 
    has_many :groups, through: :group_user_statistics 
    has_many :statistics, through: :group_user_statistics 
end 

class Group < ActiveRecord::Base 
    has_many :group_users 
    has_many :users, through: :group_users 

    has_many :group_user_statistics 
    has_many :users, through: :group_user_statistics 
    has_many :statistics, through: :group_user_statistics 
end 

class GroupUser < ActiveRecord::Base 
    belongs_to :group 
    belongs_to :user 
end 

class Statistic < ActiveRecord::Base 
    has_many :group_user_statistics 
    has_many :groups, through: :group_user_statistics 
    has_many :users, through: :group_user_statistics 
end 

class GroupUserStatistic < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :group 
    belongs_to :statistic 
end 
関連する問題