Topic: Integrate filtering into model?

I'm working on an existing app that could stand to be a bit DRYer. Right now, there's a privacy framework implemented in that certain models have a 'privacy_setting' field that's either 0, 1 or 2 (public, group, private).  A lot of queries in various controllers use something like model.find_by_sql(SELECT * from model WHERE privacy_setting = 0 OR group_id = #{logged_in_user.group} OR created_by = #{logged_in_user.id}"  (yes, I know my SQL is a bit mangled, but that's the idea).

Ideally, I'd like to refactor the model code so that any controller can call a Model.find(:all) and have the appropriate filtering applied by the model, thus cleaning up all the controllers.

Any thoughts on how can I swing this? Thanks for helping out a newbie. smile

Re: Integrate filtering into model?

alterscape wrote:

Ideally, I'd like to refactor the model code so that any controller can call a Model.find(:all) and have the appropriate filtering applied by the model, thus cleaning up all the controllers.

You'll probably wan to create a custom find in your model.  Check this out:

http://railscasts.com/episodes/4