2016-04-06 29 views
0

Bir gruptaki kullanıcılar için istatistiklerin izlenmesi için basit bir Rails uygulaması (bu benim ilk raylarım projesi) oluşturmak için çalışıyorum. Bir geçiş komut dosyası kullanarak tabloları oluşturdum ve MySql uygulamasında baktığımda her şey iyi görünüyor, ancak tüm modellerim başka bir tabloya katıldığımda veri döndürmüyor.ActiveRecord dernekleri: Doğru mu yapıyorum?

Modellerimle, taşıma komut dosyasıyla veya veri modeliyle ilgili yanlış bir şey gören var mı? Benim modelleri güncellenen

Here's my data model

+0

Sana Sen 'var Has_many – Esse

+1

through' has_many' lehine 'has_and_belongs_to_many' önlemek için denemek gerektiğini düşünüyorum iyi çalışıyor gibi görünüyor '' '' '' modelinde yanlış olan ve çalışmayan model. – Pavan

+0

@Pavan Bunun yerine ne kullanmanızı önerirsiniz? – Divide100

cevap

0

benim veri modeli burada

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 

ve oluyor: İşte

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 

benim mankenlerim: İşte

benim göç dosyası komut kodu Esse, Pavan ve AndyV'nin yorumları doğrultusunda. type` modelde `in gruplarının iş ve` has_one var: türlerini Şimdi herşey

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 
İlgili konular