About Directory location of strategy pattern in rails

37 views
Skip to first unread message

venu madhav chitta

unread,
Aug 30, 2015, 3:17:22 AM8/30/15
to Ruby on Rails: Talk

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.

Colin Law

unread,
Aug 30, 2015, 4:03:20 AM8/30/15
to Ruby on Rails: Talk
On 30 August 2015 at 04:36, venu madhav chitta
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

venu madhav chitta

unread,
Aug 30, 2015, 6:27:40 PM8/30/15
to Ruby on Rails: Talk
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]

Colin Law

unread,
Aug 31, 2015, 3:46:53 AM8/31/15
to Ruby on Rails: Talk
On 30 August 2015 at 23:27, venu madhav chitta
<venu.ch...@gmail.com> wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/c9c746c9-2957-49a3-a9ba-f52aa7ae61ec%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

venu madhav chitta

unread,
Aug 31, 2015, 7:16:06 PM8/31/15
to Ruby on Rails: Talk
Thank You so much Colin.
Reply all
Reply to author
Forward
0 new messages