Tag: 嵌套属性

嵌套模型和父validation

我有两个模型。 – Parent has_many Children ; – Parent accepted_nested_attributes_for Children ; class Parent < ActiveRecord::Base has_many :children, :dependent => :destroy accepts_nested_attributes_for :children, :allow_destroy => true validates :children, :presence => true end class Child < ActiveRecord::Base belongs_to :parent end 我使用validation来validation每个父母的孩子的存在,所以我不能救孩子没有父母。 parent = Parent.new :name => "Jose" parent.save #=> false parent.children_attributes = [{:name => "Pedro"}, {:name => […]

Rails after_initialize只在“新”

我有以下2个模型 class Sport < ActiveRecord::Base has_many :charts, order: "sortWeight ASC" has_one :product, :as => :productable accepts_nested_attributes_for :product, :allow_destroy => true end class Product < ActiveRecord::Base belongs_to :category belongs_to :productable, :polymorphic => true end 没有产品的运动就不能存在,所以在我的sports_controller.rb我有: def new @sport = Sport.new @sport.product = Product.new … end 我尝试使用after_initialize将产品的创build移动到运动模型: after_initialize :create_product def create_product self.product = Product.new end 我很快就知道,每当一个模型被实例化时(即从一个find调用), after_initialize调用after_initialize […]

accept_nested_attributes_for与belongs_to一起工作吗?

对于这个基本问题,我得到了各种相互矛盾的信息,对于我现在的问题,答案是非常重要的。 所以,很简单,在Rails 3中,是否允许使用belongs_to_attributes_for与belongs_to关系? class User < ActiveRecord::Base belongs_to :organization accepts_nested_attributes_for :organization end class Organization < ActiveRecord::Base has_many :users end 在一个视图中: = form_for @user do |f| f.label :name, "Name" f.input :name = f.fields_for :organization do |o| o.label :city, "City" o.input :city f.submit "Submit"

编辑时,RoR嵌套属性会生成重复项

我正在尝试关注Ryan Bates Rails第#196:嵌套模型表单部分1 。 Ryans的版本有两个明显的区别:1)我使用的是内置的脚手架,而且他使用的并不漂亮,2)我在运行rails 4(我不太清楚Ryans使用的是什么版本,但不是4)。 所以这就是我所做的 rails new survey2 cd survey2 bundle install rails generate scaffold survey name:string rake db:migrate rails generate model question survey_id:integer content:text rake db:migrate 然后,我将这些关联添加到模型中 class Question < ActiveRecord::Base belongs_to :survey end 所以 class Survey < ActiveRecord::Base has_many :questions accepts_nested_attributes_for :questions end 然后我添加了嵌套的视图部分 <%= form_for(@survey) do |f| %> <!– Standard […]

Rails 4 – 强大的参数 – 嵌套的对象

我有一个非常简单的问题。 但到目前为止还没有find解决办法。 所以这里是我发送给服务器的JSONstring: { "name" : "abc", "groundtruth" : { "type" : "Point", "coordinates" : [ 2.4, 6 ] } } 使用新的许可证方法,我有: params.require(:measurement).permit(:name, :groundtruth) 这不会引发错误,但创build的数据库条目包含null而不是ground_truth值。 如果我只是设置: params.require(:measurement).permit! 一切都按预期得到保存,但当然,这会杀死强参数提供的安全性。 我已经find解决scheme,如何允许数组,但不是一个使用嵌套对象的例子。 这一定是可能的,因为它应该是一个很常见的用例。 那么它是怎样工作的?

用belongs_to多态来接受belongs_nested_attributes_for

我想build立一个与accepts_nested_attributes_for的多态关系。 这里是代码: class Contact <ActiveRecord::Base has_many :jobs, :as=>:client end class Job <ActiveRecord::Base belongs_to :client, :polymorphic=>:true accepts_nested_attributes_for :client end 当我尝试访问Job.create(…, :client_attributes=>{…}给我NameError: uninitialized constant Job::Client