2012-05-07 6 views
3

今日の朝に動作していないattr_accessibleMongoid iはMongoid <a href="http://mongoid.org/docs/documents/access.html" rel="nofollow">specification</a>に言及もattr_accessibleを実施することを決定した私はmongoidはこれを克服するためにモデル</p> <p>で定義されていない属性の記録を作成していたこの奇妙な問題に目覚め

は、「アクセスなどのフィールドのリストを提供することは、単にそれらを保護 の逆である。何が エラーの原因になりますアクセスとして定義されていません。」 - 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は質量割り当てを無視します。彼らは質量割り当てでエラーを発生させません。

「保護された」セクションでは、「エラーを発生させる」が間違っており、ドキュメントがUserとPersonの不一致にさえなります。アクセシビリティに関するセクションでは、「エラーを引き起こす」という記述は間違っていますが、「保護されたものを黙って無視する」というコメントは、エラーが発生せず、質量割り当てが無視されるという手がかりを与えます。

これをサポートする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 

PartPriceRecordモデルにcustomer_idフィールドを追加する必要があります。 Userと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

おかげで、私が探していたものだと思いますが、PartPriceRecordにcustomer_idを追加するのは、CSVを解析しているときにcustomer_idが存在するため、CSVが持つことができるPartPriceRecordに属していないフィールドは、attr_accessibleを介して見ていることを防止します。 – Viren

関連する問題

 関連する問題