Tag: 嵌套属性

has_many嵌套表单,其中包含has_one嵌套表单

我目前正在尝试为模型创建一个表单,该模型具有动态数量的嵌套模型。 我正在使用嵌套表单(如RailsCasts 197中所述 )。 为了使事情变得更复杂,我的每个嵌套模型都与第三个模型有一个has_one关联,我也希望将其添加到表单中。 对于任何想要过度规范化或不正确的方法的人来说,这个例子是我所面临的问题的简化版本。 实际上,事情稍微复杂一点,这就是我们决定采取的方法。 一些示例代码来说明下面的问题: #MODELS class Test attr_accessible :test_name, :test_description, :questions_attributes has_many :questions accepts_nested_attributes_for :questions end class Question attr_accessible :question, :answer_attributes belongs_to :test has_one :answer accepts_nested_attributes_for :answer end class Answer attr_accessible :answer belongs_to :question end #CONTROLLER class TestsController < ApplicationController #GET /tests/new def new @test = Test.new @questions = @test.questions.build @answers […]

Ruby:将嵌套的Ruby哈希转换为非嵌套的Ruby哈希

现在,我有一个服务器调用踢回以下Ruby哈希: { “id”=>”-ct”, “factualId”=>””, “outOfBusiness”=>false, “publishedAt”=>”2012-03-09 11:02:01”, “general”=>{ “name”=>”A Cote”, “timeZone”=>”EST”, “desc”=>”À Côté is a small-plates restaurant in Oakland’s charming Rockridge district. Cozy tables surround large communal tables in both the main dining room and on the sunny patio to create a festive atmosphere. Small plates reflecting the best of seasonal Mediterranean cuisine are served […]

first_or_create通过电子邮件,然后保存嵌套模型

我的两个模型User和Submission如下: class User {:message => “Please enter a valid email address” } validates :email, :uniqueness => { :case_sensitive => false } end class Submission true validates :text, :length => { :minimum => 250 } validates :word_count, :numericality => { :only_integer => true } end 我有一个表格收集这两个模型所需的数据。 用户控制器: def index @user = User.new @user.submissions.build end def create […]

validation嵌套属性的数量

我有一个嵌套属性的模型: class Foo < ActiveRecord::Base has_many :bar accepts_nested_attributes_for :bar end 它工作正常。 但是我想确保每一个Foo,我至少有两个Bar。 我无法访问我的validation中的bar_attributes ,所以我似乎无法validation它。 有没有干净的方法呢?

accepts_nested_attributes_for忽略空值

我有 class Profile has_many :favorite_books, :dependent => :destroy has_many :favorite_quotes, :dependent => :destroy accepts_nested_attributes_for :favorite_books, :allow_destroy => true accepts_nested_attributes_for :favorite_quotes, :allow_destroy => true end 我有一个动态表单,你按’+’添加新的textareas来创建新的collections夹。 我想要做的是忽略空白的,我发现在更新控制器中比非嵌套属性更难排序。 我暂时拥有的是删除空记录的after_save回调中的黑客攻击。 什么是最容易忽略这些空白对象的轨道方式? 我不想要validation和错误,只是一个无声的删除/忽略。

使用嵌套属性在rails中的多对多关系的下拉菜单

我通过多对多关联有三个表:超市,产品和供应。 每个超市都可以容纳许多产品,每个产品都可以在许多超市销售。 该关联是通过Supply-model构建的。 超级市场: class Supermarket :supplies accepts_nested_attributes_for :products end 产品: class Product :supplies accepts_nested_attributes_for :supermarkets end 通过供应协会: class Supply < ActiveRecord::Base attr_accessible :supermarket_id, :product_id belongs_to :supermarket belongs_to :product end 我创建了脚手架,并填充了超市桌。 在我的产品表单中,我想使用一个(或多个)下拉菜单来选择相应的超市名称。 目标是创建一个新产品,同时通过Supply-table创建关联。 如果我想从那里选择相应的超市,那么代码在产品的forms和/或控制器中应该是什么样的?