1

I am very new to ruby and rails. I am coming from a C++ background, so ruby and rails are a little vexing.

I have designed a rails application that works as a queue. The web API allows items to be pushed into and popped out of the queue. Currently this lives as a specific application with a Jobs model and JobQueueItems model that belongs_to Jobs. However, the mechanism of the queue is not specific to Job at all. It just relies on a foreign key for the item in the queue. How can I abstract this behavior so the next time I want an AnimalQueue or a CustomerQueue I can just create one in my application?

I know I can make a generator which will create the model code with names and methods like "Job" hardcoded in, then call

rails g MyCoolQueue Job

but this seems quite complicated. There are routes and names and views and strong parameters methods to be set up. Is there another way? Can use inheritance or interfaces to make this work? Something like:

class QueueItem < ActiveRecord::Base
end

class JobQueueItem < QueueItem
    belongs_to :Job
end

class QueueItemsController < ApplicationController
    # all my queue controls, reference the foreign key and associated model in a generic way...
end

class JobQueueItemsController < QueueItemsController
    # suddenly I have the controls to queue jobs?
end

# And also some views that generically reference the necessary fields and models in a QueueItem?
# Suddenly I have views for JobQueueItems?

How does one go about making an abstract data type, tool, or other abstract relationship that is easily inserted in to a rails app? One could imagine any number of scenarios where behavior and relationships can be abstracted and only need the names of models and fields to be filled in. I'm sure experienced rails developers don't recode everything from scratch every time or just copy and paste. As I said before, I am new to ruby and rails, so I would like to learn the standard RoR way to do something like this. Is code generation the only way?

Thanks!

user487100
  • 918
  • 1
  • 11
  • 22
  • 1
    There are any number of ways this could be done; it'd probably work as a [polymorphic association](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations), or a relatively simple module you could mix in to the AR class, or... – Dave Newton Nov 07 '14 at 18:28
  • I can see how a polymorphic association could let me have a QueueItem model which could refer more than one type of data. But How could I use the polymorphic association to just create two different QueueItem models when I want them. What if I wanted a queue for users and a queue for jobs in my application? Almost all the code is the same. I like the idea of a mixin, but I don't know how to refer to fields generically. The name of variables seems important in rails due to all the reflection, naming conventions, and automatically generated methods. – user487100 Nov 07 '14 at 18:43
  • Possible duplicate of [Rails apps that decouple ActiveRecord from Business Logic](http://stackoverflow.com/questions/14194988/rails-apps-that-decouple-activerecord-from-business-logic) – Paul Sweatte Feb 08 '17 at 16:57

0 Answers0