1

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

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!
4

1 回答 1

0

这样做应该有效:

User.create!( { 
  "email" =>  "asdf@asdf.com", 
  "payments_attributes" => [{paid: false, method: "bank"}] 
} )
于 2013-11-19T20:32:51.573 回答