今天早上我與在mongoid是創造一個紀錄爲未在模型Mongoid attr_accessible不工作
爲了克服這一點,我決定實施attr_accessible所定義的屬性,這個奇怪的問題,喚醒還提到在Mongoid specification
「提供了可訪問字段列表是一個簡單的 保護他們的倒數。任何未被定義爲訪問將導致 錯誤。」 - Mongoid規格
想想都將正常工作,我創建我被插入虛擬的記錄,太出乎我的意料的反對沒有定義爲訪問上面
「任何的聲明將導致錯誤
這裏我的模型結構
class PartPriceRecord
include Mongoid::Document
field :supplier_id,type: Integer
field :part_number,type: String
field :part_description, type: String
field :core_indicator,type: String
field :us_part_price,type: Float
field :us_core_price,type: Float
field :us_fleet_price,type: Float
field :us_distributor_price,type: Float
field :ca_part_price,type: Float
field :ca_distributor_price,type: Float
field :ca_core_price,type: Float
field :ca_fleet_price,type: Float
field :basic_file_id,type: Integer
index :part_number, unique: true
validates_presence_of :supplier_id
validates_presence_of :part_number
#validates_uniqueness_of :part_number
validates :part_number ,:format => { :with => /^[a-z0-9A-Z\s*-]+[-a-z0-9\s-]*[a-z0-9\s*-]+$/i ,:message => "Only AlphaNumeric Allowed" }
validates :supplier_id, :format => { :with => /^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i , :message => "Only Alphanumeric Allowed" }
#validates :part_description,:presence => true
validates :part_description,:format => { :with => /^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i ,:message => "Only Alphanumberic Allowed"} ,:allow_nil => true
validates :core_indicator ,:inclusion => { :in => %w(Y N),
:message => "%{value} is not a valid Coreindicator must be Y | N"
} ,:allow_nil => true,:allow_blank => true
validates :us_part_price,:us_core_price,:us_fleet_price,:us_distributor_price,:ca_part_price,:ca_core_price,:ca_fleet_price,:ca_distributor_price ,:format => { :with => /^([0-9]+(\.([0-9]{2}|[0-9]{1}))?)$/ ,:message => "should look like money" } ,:allow_nil => true,:allow_blank => true
@@required_attributes =[:supplier_id,:part_number,:part_description,:core_indicator,:us_part_price,:us_core_price,:us_fleet_price,:us_distributor_price,:ca_part_price,:ca_core_price,:ca_fleet_price,:ca_distributor_price]
@@not_required_attributes = ["_id","basic_file_id"]
cattr_reader :required_attributes,:not_required_attributes
attr_accessible :supplier_id,:part_number,:part_description, :core_indicator,:us_part_price,:us_core_price,:us_fleet_price,:us_distributor_price,:ca_part_price,:ca_distributor_price,:ca_core_price,:ca_fleet_price,:basic_file_id
end
,並在這裏,我從我的控制檯創建
ruby-1.9.2-head :003 > PartPriceRecord.count()
=> 260317 ## initial count before creating a new record
ruby-1.9.2-head :004 > p1 = PartPriceRecord.new(:customer_id => "One",:part_number => "ASA",:supplier_id => "Supp")
=> #<PartPriceRecord _id: 4fa77921d2d8d60e39000002, _type: nil, supplier_id: "Supp", part_number: "ASA", part_description: nil, core_indicator: nil, us_part_price: nil, us_core_price: nil, us_fleet_price: nil, us_distributor_price: nil, ca_part_price: nil, ca_distributor_price: nil, ca_core_price: nil, ca_fleet_price: nil, basic_file_id: nil>
ruby-1.9.2-head :005 > p1.save
=> true ## Record got created
ruby-1.9.2-head :006 > PartPriceRecord.count()
=> 260318 ## Count indicating record was created
任何想法記錄,爲什麼會這樣呢?
感謝
那麼,那個記錄有什麼問題? –
@Sergio customer_id屬性未在attr_accessible中定義 – Viren
而且它沒有在字段中列出,爲什麼? –