ActiveResource Errors

tags: [activeresource] [jason]

Learning about ActiveResource Errors

I’m working on a TransferMembership action that creates new wine club memberships in a handy new subscriptions service based on current memberships in our application. One of the things that was confusing me is getting errors on a new membership instance.

membership = Subscription::Membership.build # hits service
membership.save # => false, doesn't hit service.  Must know not to send over an invalid object
membership.valid? # => false
membership.errors.full_messages # => [] !!! Why you no error messages?

I’m thinking we need to add validations in the application to get things to work the way I think it should.

module Subscription
  class Base < ActiveResource::Base

...

module Subscription
 class Membership < Base

    has_many :orders, class_name: 'WineClub::Order'
    belongs_to :club, class_name: 'WineClub::Club'
    belongs_to :club_variant, class_name: 'WineClub::ClubVariant'
    belongs_to :plan, class_name: 'WineClub::Plan'

    validates :plan, presence: true
    validates :mofo_id, presence: true

This got things to work the way I thought it should

membership = Subscription::Membership.build # hits service
membership.save # => false, doesn't hit service.  Must know not to send over an invalid object
membership.valid? # => false
membership.errors.full_messages # => ["Plan can't be blank", "Mofo can't be blank"]

I’m going to toss this to people that know things. See how it goes.