Rails "assign_attributes" does not assign nested models

I have two models with the following structure:

class Wallet < ActiveRecord::Base
  include ActiveModel::Validations
  has_one :credit_card
  accepts_nested_attributes_for :credit_card

  validates :credit_card, :presence => true
  validates_associated :credit_card
  ...
end

class CreditCard < ActiveRecord::Base
  include ActiveModel::Validations
  belongs_to :wallet

  validates :card_number, :presence => true
  validates :expiration_date, :presence => true
  ...
end

I am testing the functionality of my application with RSpec and I noticed something strange. If I create a hash with attributes that do not meet the criteria for checking my nested model (for example, nil card_number), and then try to make a call update_attributes, then what I get in the Wallet object with an invalid nested CreditCard model and the corresponding errors. This is the correct, expected behavior.

Hash assign_attributes, save ( , update_attributes, . ? ?

+5
3

- include ActiveModel::Validations, ActiveRecord::Base.

- update_attributes assign_attributes , .

attr_accessible, attr_protected, with/without_protection, ,

{'credit_card_attributes' => {'card_number' => ''}}

. , , .

, ,

Wallet.new(hash_with_attributes).valid?

.

+4

, Strong Params ( Rails 4) , , , nested_attributes .

, . fooobar.com/questions/181611/...

save update_attributes - . , , , . , .

+1

As I understand it, assign_attributes skips security checks.

Is there a difference between = and assign_attributes in Rails 3?

0
source

All Articles