attr_accessible 似乎不再适用于我的模型.
在Rails 4中允许批量分配的方法是什么?
我有一个功能正常的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) 我有一个Bill对象,它有很多Due对象.该Due对象也属于Person.我想要一个可以在一个页面中创建Bill及其子项的表单Dues.我正在尝试使用嵌套属性创建表单,类似于此Railscast中的表单.
相关代码如下:
due.rb
class Due < ActiveRecord::Base
belongs_to :person
belongs_to :bill
end
Run Code Online (Sandbox Code Playgroud)
bill.rb
class Bill < ActiveRecord::Base
has_many :dues, :dependent => :destroy
accepts_nested_attributes_for :dues, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
bills_controller.rb
# GET /bills/new
def new
@bill = Bill.new
3.times { @bill.dues.build }
end
Run Code Online (Sandbox Code Playgroud)
票据/ _form.html.erb
<%= form_for(@bill) do |f| %>
<div class="field">
<%= f.label :company %><br />
<%= f.text_field :company %>
</div>
<div class="field">
<%= f.label :month …Run Code Online (Sandbox Code Playgroud)