has_manyãªåãã¼ãã«ã«ãã¼ã¿ãä½æããbuildã¡ã½ãã
å
ã»ã©æ°äººãã質åã«æ¥ããã¦ãbuildã®ä½¿ãæ¹ã§æ©ãã§ãã¨èãããã®ã§ããã
ãbuildã£ã¦ãã£ãã£ãï¼newãããï¼ãã£ã¦ç¶æ
ã§ããã
ããã¦è²ã ã¨æ¤è¨¼ããã¨é¢ç½ãã¡ã½ããã ã¨åãã£ãã®ã§ç´¹ä»
class Parent < ActiveRecord::Base has_many :childs end class Child < ActiveRecord::Base belongs_to :parent has_many :child_childs end class ChildChild < ActiveRecord::Base belongs_to :child validate_presence_of :name end
ã¨ãã親åé¢ä¿ããã£ãã¨ãã¦
parent = Parent.new child = parent.childs.build child_child = chiild.child_childs.build(:name => "test")
ã¨ããã¨ã
parent.childs = [child]
child.child_childs = [child_child]
ã¨ããç¶æ
ã«ãªãã¾ããã
ããã¦ãparent.saveã§ãchildãchild_childãèªåä¿åã
ãããã¡ãã£ã¨ä¾¿å©ã ã
ã¡ãªã¿ã«ã
parent = Parent.new child = parent.childs.build child_child = chiild.child_childs.build p parent.save #=> false
ã¨è¨ã風ã«ãé¢é£ãã¼ãã«ããã§ãã¯ãã¦ããã模æ§ã