1

I'm trying to use Rails validations for a form text box to see if the text entered matches any existing values in a specific column of a table in the database. Is this possible with Rails?

Basically, like this:

User enters 'Foobar'

Table column values: 'Foobar,test,house,random'

Validation does not pass because 'Foobar' is already in the database.

Thanks! dwmcc

1 Answer 1

1

You can do this in your model with something like validates_uniqueness_of :name or new in Rails 3 you can do multiple validations inline with validates :name, :presence => true, :uniqueness => true

From the Rails API.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, two more questions: How can I make the matching case insensitive so that the application will not accept 'Foobar' because 'FOOBAR' already exists in the database? And how can I validate the uniqueness of the field based on another table? To clarify, how can I validate that 'Foobar' does not exist in another table in my database? Thanks!
As for the second part of your question, I do not know. I think that is complex enough to warrant a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.