Zoinks10
Zoinks10

Reputation: 629

Where should I place the score variable when allocating a score to a Model/Object in Ruby/Ruby on Rails?

I have a model in my Rails app for a SalesOpportunity, and running a SWOT analysis (Strength, Weakness, Opportunity, Threat) to decide how good the SalesOpportunity is. Swots belong_to SalesOpportunities and therefore in the SalesOpportunity Model I have a method called update_swot_score which iterates through the Swot objects and calculates a score based on parameters I'm feeding in. All of this works fine.

What I'm wondering is whether I need to add a field to my SalesOpportunity model (let's call it swot_score for simplicity) and to update the instance variable at the end of the update_swot_score method using @swot_score = "results of my calculation", or whether I can directly access a result of the update_swot_score method (ideally in my view - I'll display different partials depending on the result).

What is the Rails way of doing this? Is there a performance efficiency to be gained by using either method?

Upvotes: 0

Views: 45

Answers (1)

Milind
Milind

Reputation: 5112

i will suggest to add in the db as a dedicated column to store score...there are few good reasons as why you should do it:-

  1. dedicated column to store only score
  2. easily maintainable even you decide to add/edit new score type in future
  3. Can be called/updated/modified anywhere throughout your application
  4. can use callbacks in future as well(on_create,on_update) as its in a Model
  5. Performance wise,its awesome as you can cache it..can also counter_cache and expire too.

Hope it helps

Upvotes: 1

Related Questions