About Directory location of strategy pattern in rails

I am implementing strategy pattern in Rails where I have Models like User, Item, Category and need to recommend items for the users depending on various algorithms (strategies) that user selects in view.

I am having a Recommend class which has an interface of recommend(user_id, strategy) and returns array of item_id. The strategy in recommend will be decided at runtime depending on the option user selects in the view. I have placed the recommend interface in /lib directory and the strategies in /lib/strategy directory. The strategies or algorithms right now will do SQL queries to give recommendations which is naive.

I want to make sure if I placed the files in proper directories or Should I need place the recommend class and all the strategies in models or app/services or any other?. I am really confused.

Are there database tables behind the recommend and strategy code or are they just code? You say there are Items but have not told us what an Item is.

Colin

No they just use existing tables, do some joins and generate the item recommendations. Their interface looks like: recommendations(user_id) and returns [item_id]

No they just use existing tables, do some joins and generate the item recommendations. Their interface looks like: recommendations(user_id) and returns [item_id]

First if you are using joins manually then it may be that you have specified the associations incorrectly as it should only rarely be necessary to specify the joins. In addition you said in the original post that you were using sql to query, this is almost certainly not right unless you are doing something rather complex that the rails activerecord interface will not handle. If you want advice on that then ask a separate question showing the query you are performing.

So if I understand correctly you have some code that references several tables but it does not seem appropriate to just put the code inside one of the models. In such situations I would either make it a model which is not derived from ActiveRecord or put it in a module in lib. Whichever seems most appropriate. Probable a model in your case. Don't get stressed over exactly where you put things however, there are no hard rules, just do whatever seems most appropriate for your case.

Colin

Thank You so much Colin.