Forum: Ruby on Rails tricky form validation

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Brendon G. (Guest)
on 2006-02-02 09:58
(Received via mailing list)
Hi list,

I want to create a form validation in my Marketing Model.
I want to raise an error only if a previous marketing campaign for a
property has not been completed (ie. ended_on IS NULL)

I managed to get this working in the Controller using something this:
if Marketing.count("property_id = #{@marketing.property_id} AND ended_on
IS NULL") > 0

However I assume validation should be in the Model so this is where I am
stuck.. I am guessing ill have to use validates_uniqueness_of with a
:if => '???'

any ideas?
Brendon G. (Guest)
on 2006-02-02 13:07
(Received via mailing list)
Brendon G. wrote:
> ...
def validate_on_create
	if Marketing.count("property_id = #{property_id} AND ended_on IS NULL")
> 0
		errors.add("property_id","is already being marketed, first end the
current campaign.")
This topic is locked and can not be replied to.