我是 Rails 新手,对模型关联有点困惑。这里是需要做什么的简要说明。我有一个User
模型,那个has_many project
,和Project
has_many upload
,Upload
belongs_to project
和Project
belongs_to user
。
到目前为止它正在工作,用户可以访问那里的项目,并从该项目访问那里上传。问题是,任何用户都可以访问每个用户的项目和上传。通过更改 url localhost:3000/projects/9/uploads/57 我需要使项目和上传只能由正确的用户访问(用户如何创建项目和上传)
架构.rb
create_table "projects", force: true do |t|
t.string "name"
t.string "comment"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "uploads", force: true do |t|
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "edl_file_name"
t.string "edl_content_type"
t.integer "edl_file_size"
t.datetime "edl_updated_at"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
end
用户模型
has_many :project
has_many :upload, :through => project
项目模型
belongs_to :user
has_many :upload, :dependent => :destroy
上传模型
belongs_to :project
has_one :user, :through => :project
路线.rb
resources :users
resources :projects do
resources :uploads do
end
end
也许是关系?你会怎么做?