Redirect to www subdomain via DNS
Solution 1:
There is no way to do what you are asking for in DNS.
DNS simply resolves a hostname to an IP address there are no 'redirect' features
If you want users to only use your app via www.example.com you will have to do it in the app using the rack rewrite tip you linked to.
Solution 2:
Assuming you're okay with changing your DNS servers, you can use DNSimple's "URL" DNS type. This will take care of your issue this at the DNS level.
It is mentioned in this Heroku article on Avoiding Naked Domains.
Solution 3:
If you are on heroku the simplest way is a wildcard domain
heroku addons:add custom_domains:wildcard
This will redirect all subdomains *.example.com, including www to example.com.
If you have root access to your name server you must add:
www IN A xxx.xxx.xxx.xxx
or
* IN A xxx.xxx.xxx.xxx
for a wildcard where xxx.xxx.xxx.xxx is your IP, or
www IN A myapp.heroku.com.
* IN A myapp.heroku.com.
if you have an unknown/dynamic IP