我正在研究一個由學校,學生和文章組成的項目。當我開始開發我的文章控制器時,我意識到必須有更好的方式來完成我的目標,但我沒有經驗來弄清楚。基本上我想要做的是讓我的用戶註冊某個學校。然後,一旦用戶登錄,他應該能夠看到他的學校和類別的所有文章。這裏是我的設置,任何意見,將不勝感激:Rails複雜的數據庫關係
school.rb:
class School < ActiveRecord::Base
has_many :users
has_many :articles, :through => :users
attr_accessible :name
end
user.rb:
class User < ActiveRecord::Base
belongs_to :school
has_many :articles
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :provider, :uid, :school_id
end
category.rb:
class Category < ActiveRecord::Base
attr_accessible :name
has_many :articles
accepts_nested_attributes_for :articles
end
文章。 rb:
class Article < ActiveRecord::Base
attr_accessible :body
belongs_to :category
belongs_to :user
end
這似乎是一個很好的設置來完成我的目標?先謝謝了。
用戶和文章之間的關係是什麼?用戶是否創建文章,或只是閱讀他們(或兩者)? –