일부 Rspec 내용을 배우고 실수로 모델 클래스에 일부 코드를 입력하여 오류가 발생할 것으로 예상했습니다. 그러나 놀랍게도 거기에는 없었습니다.Ruby 클래스 정의에 임의 코드를 추가 할 수있는 이유는 무엇입니까?
Arman$ rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > a = Address.new
Hey! I shouldn't be able to do this
=> #<Address id: nil, street: nil, city: nil, zip: nil, country: nil, person_id: nil, created_at: nil, updated_at: nil>
1.9.3p194 :002 >
왜 이상한 코드가 클래스 정의 내부에 추가되는 불평 루비되지 않습니다
class Address < ActiveRecord::Base
attr_accessible :city, :country, :person_id, :street, :zip
validates_presence_of :city, :zip, :street
before_save :setDefaultCountry
# -- Beginning strange code --
if true
puts "Hey! I shouldn't be able to do this"
end
# -- End of strange code --
private
def setDefaultCountry
if self.country.blank?
self.country = "US"
end
end
end
이 레일 콘솔 출력은?
사실'attr_accessible'는'해시'입니다. 그래서 나는 그것을 필드를 초기화하는 것으로 본다. – Roman
"해시"란 무엇을 의미합니까? –
'puts attr_accessible.class'를 시도 할 때'Hash'를 출력합니다. – Roman