Id like to create a model in rails that does not correlate with a table in the database. Instead, the model should dynamically retrieve aggregate data from other models.
Example:
I have a restaurant model stored in a restaurant table in the database. I like to have a RestaurantStats model where I can run RestaurantStats.find_total_visitors, or RestaurantStats.find_time_spent, etc ... on it, and it returns a set of RestaurantStats models, each of which:
[: restaurant_id ,: stat_value]
Obviously, in each method find ... stat_value will mean something else (for find_time_spent it will take a few seconds, for find_total_visitors there will be the number of visitors). The idea would be to return the top 100 restaurants by time or total number of visitors.
While I'm creating a model (not inherited from ActiveRecord)
class RestaurantStats
attr_reader :restaurant_id
attr_reader :stat_value
def self.find_total_visitors ...
def self.find_time_spent ...
end
The question is how to define the functions find_total_visitors, find_time_spent in rails y so that they fill the fields restaurant_id, stat_value?
source
share