未保存嵌套的属性



我有一个User类,它可以有许多付款。用户将被保存,但付款不会。

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  attr_accessor :payments, :payments_attributes
  attr_accessible :payments_attributes, :payments
  has_many :payments, :inverse_of => :user, :autosave => true
  accepts_nested_attributes_for :payments, allow_destroy: false
end
class Payment < ActiveRecord::Base
  # it looks that I do not need the attr_accessor methods
  #attr_accessor :method, :paid, :amount, :creditcard, :security_code, :expires_at
  attr_accessible :method, :paid, :amount, :creditcard, :security_code, :expires_at
  attr_accessible :created_at, :user_id
  belongs_to :user, :inverse_of => :payments
  validates_presence_of :method
end

我尝试过这种方法:

User.create!( { email: "asdf@asdf.com", payments: {paid: false, method: "bank"} } )

有没有一个解决方案可以在不通过的情况下做到这一点:

u = User.new(params)
u.payments(payments_params)
u.save!

这样做应该有效:

User.create!( { 
  "email" =>  "asdf@asdf.com", 
  "payments_attributes" => [{paid: false, method: "bank"}] 
} )

相关内容

  • 没有找到相关文章

最新更新