0
我正在構建一個簡單的Rails應用程序(這是我的第一個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_and_belongs_to_many'贊成的has_many through' – Esse
的'你'的has_many:groups'在'type'模型,你有'HAS_ONE:類型'在'組'模型中是錯誤的,並且不起作用。 – Pavan
@Pavan你會推薦使用什麼? – Divide100