Colin L. wrote:
2009/9/14 Kendall B. [email protected]:
saved to the database. I’d like it to not save.
Am I missing something simple?
Are you sure it is getting saved? Are you checking the return from
save, it should return false if the save fails. If you still think it
is not functioning correctly post the relevant section of controller
code here.
Colin
Sure.
Yeah, it’s getting saved by an INSERT statement. Here’s the controller
code:
def create
@activity = Activity.find(params[:activity_id])
@payment_application =
@activity.payment_applications.new(params[:payment_application])
respond_to do |format|
if @payment_application.save
format.html { redirect_to(@payment_application) }
format.js { index }
format.apply_to_activity { index }
format.xml { render :xml => @payment_application, :status =>
:created, :location => @payment_application }
else
format.html { render :action => “new” }
format.js { render :action => “new”, :status =>
:unprocessable_entity }
format.apply_to_activity { index }
format.xml { render :xml => @payment_application.errors,
:status => :unprocessable_entity }
end
end
end
And the model code:
class PaymentApplication < ActiveRecord::Base
belongs_to :activity
belongs_to :payment
after_create :apply_full, :if => :apply_full_payment
attr_accessor :apply_full_payment
attr_accessor :amount_as_str
acts_as_ferret :fields => { :payment_id => { :store => :yes },
:check_num => { :store => :yes },
:comments => { :store => :yes } }
validate :total_applications_cannot_exceed_payment_amount
validate :cannot_exceed_activity_price
validates_numericality_of :amount
def amount=(value)
unless value.blank?
write_attribute(:amount, Accounting.string_as_int(value))
else
write_attribute(:amount, nil)
end
end
protected
def apply_full
write_attribute(:amount, self.payment.amount)
write_attribute(:comments, self.payment.comments)
self.save
end
def total_applications_cannot_exceed_payment_amount
if self.payment
if self.payment.payment_applications.sum(:amount) + self.amount
self.payment.amount
self.errors.add(:amount, “of all payment applications cannot
exceed total payment amount.”)
end
end
end
def cannot_exceed_activity_price
if self.payment
if self.payment.payment_applications.sum(:amount) + self.amount
self.activity.price
self.errors.add(:amount, “cannot exceed activity price.”)
end
end
end
end
Mucho thankso for the response.