我有一个功能正常的Rails 3应用程序使用has_many:通过关联,当我将其重新设置为Rails 4应用程序时,让我从Rails 4版本中的相关模型中保存ID.
这两个版本的三个相关模型是相同的.
Categorization.rb
class Categorization < ActiveRecord::Base
belongs_to :question
belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)
Question.rb
has_many :categorizations
has_many :categories, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
Category.rb
has_many :categorizations
has_many :questions, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
在这两个应用程序中,类别ID都会像这样传递到create动作中
"question"=>{"question_content"=>"How do you spell car?", "question_details"=>"blah ", "category_ids"=>["", "2"],
Run Code Online (Sandbox Code Playgroud)
在Rails 3应用程序中,当我创建一个新问题时,它会插入问题表,然后插入到分类表中
SQL (82.1ms) INSERT INTO "questions" ("accepted_answer_id", "city", "created_at", "details", "province", "province_id", "question", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["accepted_answer_id", nil], ["city", "dd"], ["created_at", Tue, 14 May 2013 17:10:25 UTC +00:00], ["details", "greyound?"], ["province", …
Run Code Online (Sandbox Code Playgroud)