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
、ここでは私のデータモデルは、私は私のモデルを更新
を正常に動作しているようだ私はあなたが 'にhas_manyを持って – Esse
through'' 'にhas_manyの賛成でhas_and_belongs_to_many'を回避しようとすべきだと思います'グループ'モデルでは間違っていて動作しません。 – Pavan
@Pavan代わりに何をお勧めしますか? – Divide100