views:

34

answers:

1

We allow users to add domains to an active record model such as User.domain and find the users by domain using User.find_by_domain.

In the future we want to allow users to enter *.example.com as their domain and allow User.find_by_subdomain('sub1.example.com') and User.find_by_subdomain('sub2.example.com') to work. However we also want example.com to match example.com directly if a user does not use a wildcard like *.example.com in the model. We would need to be able to say User.find_by_subdomain('sub1.example.com') not User.find_by_subdomain('*.example.com')

This would also have to work with domains like sub.example.co.uk and we would ideally like an efficient query that could run in the database. Avoiding database specific queries would be nice but Postgres is the current production DB.

Thanks in advance as we have been racking our brains with this one for a little bit.

A: 

Try this like optimization:

http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks#LIKE_optimalization

Denis