object.valid? returns false but object.errors.full_messages is empty

Posted by user549563 on Stack Overflow See other posts from Stack Overflow or by user549563
Published on 2010-12-21T12:25:13Z Indexed on 2010/12/22 7:54 UTC
Read the original article Hit count: 193

Hello

I'm confuse about objects that I can't save, simplified model is

class Subscription < ActiveRecord::base
    belongs_to :user, :class_name => "User", :foreign_key => "user_id"
has_many :transactions, :class_name => "SubscriptionTransaction" 

validates_presence_of :first_name, :message => "ne peut être vide"
validates_presence_of :last_name, :message => "ne peut être vide"
validates_presence_of :card_number, :message => "ne peut être vide"
validates_presence_of :card_verification, :message => "ne peut être vide"
validates_presence_of :card_type, :message => "ne peut être vide"
validates_presence_of :card_expires_on, :message => "ne peut être vide"

attr_accessor :card_number, :card_verification

validate_on_create :validate_card

    def validate_card 
    unless credit_card.valid?
        credit_card.errors.full_messages.each do |message|
            errors.add_to_base message
        end
    end
end

def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
        :type => card_type,
        :number => card_number,
        :verification_value => card_verification,
        :month => card_expires_on.month,
        :year => card_expires_on.year,
        :first_name => first_name,
        :last_name => last_name
    )
end
end 

and in my subscription_controller

if subscription.save
     # do something
else
     debugger # means breakpoint where i try subscription.errors.full_messages
     # do something else
end 

I tried to use ruby-debug for this adding a breakpoint before. And subscription.valid? return false which explains that ActiveRecord doesn't allow the save method. Unfortunately i can't know why the object is invalid.

subscription.errors.full_messages # => []

I'm stucked, if you have any idea, thank you.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby