0
我有3種型號保存對象ID爲嵌套的has_many通過模型
class User < ActiveRecord::Base
has_many :projects
has_many :project_files, through: :projects
class Project < ActiveRecord::Base
belongs_to :user
has_many :project_files
class ProjectFile < ActiveRecord::Base
belongs_to :project
當我使用像創建project_file
:
@project = current_user.projects.find(params[:id])
@project.project_files.new
它沒有用戶ID保存到project_file記錄,但它確實保存了項目ID。
我希望能夠通過調用@project_file.user
得到它的用戶訪問,並返回ID
我認爲你的語法:'@project.project_file.new'不正確,你確定它的工作?它應該是:'@ project.project_files.new'。 – Surya 2014-10-11 16:29:33
道歉我在這個例子中寫錯了,我現在編輯了代碼。我在代碼 – 2014-10-11 16:31:58
中有'@project.project_files.new'對不起,但'current_user.project.find(params [:id])'也應該是:'current_user.projects.find(params [:id])' – Surya 2014-10-11 16:32:06