errors.add_to_base not actually adding any errors to base?
105 views
Skip to first unread message
Jeff
unread,
Jan 14, 2008, 6:18:51 PM1/14/08
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Talk
I checked the API docs and found nothing about add_to_base gotchas..
but when adding an error to the base of an ActiveRecord object, it
essentially does nothing. The save/valid? methods work as if there
were no errors, and of course the view does not display them. Code
basically goes like this:
@order = account.orders.build
@order.errors.add_to_base("Must select at least one item")
if @order.save
...
also tried @order.valid? instead of save
Both return true and the record is saved anyway. Any ideas?
I am running Rails 1.2.6 as a gem.
Jeff
unread,
Jan 14, 2008, 10:09:32 PM1/14/08
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Talk
I figured this out. The "save" and "valid?" methods call errors.clear
before running, which was dumping the errors I had added.
Should have thought to put it in the model anyway, it is business
logic after all. This works:
def validate
if items.empty?
errors.add_to_base("Must select at least one item.")
end
end