2012-05-07 47 views
3

今天早上我與在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 

任何想法記錄,爲什麼會這樣呢?

感謝

+0

那麼,那個記錄有什麼問題? –

+0

@Sergio customer_id屬性未在attr_accessible中定義 – Viren

+0

而且它沒有在字段中列出,爲什麼? –

回答

2

你的問題是有效的 - 看來該文件是不一致的,不完全正確,而且有些過時,從下面的測試和Mogoid代碼粗略瀏覽。

attr_protected或NOT attr_accessible的字段忽略質量分配;他們不會在批量分配時提出錯誤。

在受保護的部分中,「引發錯誤」不正確,文檔甚至與用戶和用戶不匹配。在可訪問部分,「會導致錯誤」是不正確的,但註釋「默默忽略受保護的」給出了一條線索,即不會引發錯誤,並且忽略質量分配。

這裏是一個來自mongoid/spec/mongoid/attributes_spec.rb的支持這個的片段。

describe ".attr_accessible" do 

    context "when the field is not _id" do 

    let(:account) do 
     Account.new(number: 999999) 
    end 

    it "prevents setting via mass assignment" do 
     account.number.should be_nil 
    end 
    end 
... 
end 

您必須將字段customer_id添加到您的PartPriceRecord模型中。用戶和PartPriceRecord的測試如下。希望這有助於。

require 'test_helper' 

class PartPriceRecordTest < ActiveSupport::TestCase 
    def setup 
    User.delete_all 
    PartPriceRecord.delete_all 
    end 

    test "User" do 
    assert_equal(0, User.count()) 
    # Set attributes on a user properly. 
    user = User.new(first_name: "Corbin") 
    assert_equal("Corbin", user.first_name) 
    user.attributes = { first_name: "Corbin" } 
    assert_equal("Corbin", user.first_name) 
    user.write_attributes(first_name: "Corbin") 
    assert_equal("Corbin", user.first_name) 

    # Attempt to set attributes a user, raising an error. # <-- This documentation is incorrect, no error is raised 
    #user = User.new(first_name: "Corbin", password: "password") 
    user.attributes = { first_name: "Corbin", password: "password" } # inaccessible field is forced to nil 
    assert_equal("Corbin", user.first_name) 
    assert_equal(nil, user.password) 
    user.write_attributes(first_name: "Corbin", password: "password") # inaccessible field is forced to nil 
    assert_equal("Corbin", user.first_name) 
    assert_equal(nil, user.password) 
    end 

    test "PartPriceRecord" do 
    assert_equal(0, PartPriceRecord.count()) 
    p1 = PartPriceRecord.new(:customer_id => "One",:part_number => "ASA",:supplier_id => "Supp") 
    assert_equal(nil, p1.customer_id) 
    p1.save 
    assert_equal(1, PartPriceRecord.count()) 
    assert_equal(nil, PartPriceRecord.find(p1.id).customer_id) 
    end 
end 
+0

謝謝我猜我在找什麼,但是將一個customer_id添加到PartPriceRecord是我無法做到的,因爲customer_id只是一個可以在我解析CSV時出現的關鍵字,因此在任何時候CSV都可以有任何不屬於PartPriceRecord的字段都會阻止正在查看attr_accessible的內容 – Viren