Forum: Ruby on Rails tricky form validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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:
> ...
>
SOLVED!
(Model)
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.")
	end
end
This topic is locked and can not be replied to.