0

I'm developing app that help people overview results of their training. In different disciplines different calculations. And I need to store discipline in object related to training.

I have to use this values in code to programmaticaly set fields of another objects, calculate results depend on it, and later to compare with it.

For example I have model Discipline and three records with names

  • Speed on 400 m
  • Speed on 1 km
  • Speed on 4 km

So it's some kind of constants and I'm just wondering what is the best practice to declare and access them?

Or may be is there any way to declare predefined objects?

Aleksandr K.
  • 1,338
  • 14
  • 21
  • @Зелёный, done it. 1. Search and research - done before, 2. title - seems to be ok, 3. question - improved. Something else? – Aleksandr K. Nov 14 '14 at 18:39
  • What you mean then say `constant`? in Rails you can't create constant from class instance(like `ActiveRecord` object). I suggest you read about [ActiveRecord Association Basics](http://guides.rubyonrails.org/association_basics.html) i think your case achieved through simple relation if i right understand your question. – Roman Kiselenko Nov 14 '14 at 18:45
  • @Зелёный, read it. I think I should ask question in another way, because answer below gaves me direction. It's like enum, don't know why I decide to make it as AR model. So, may be you know is there any way to define enum in module and use it as AR fields value? – Aleksandr K. Nov 14 '14 at 18:49
  • enum already model field, maybe you mean virtual attributes? Can you show some part of code and attempts this should be helpful. – Roman Kiselenko Nov 14 '14 at 18:53
  • @Зелёный, no. Now I have model Discipline and it used in few another Models. So I want to replace model Discipline to enum and define values in one place and use it in all Models that needs discipline as a field? – Aleksandr K. Nov 14 '14 at 18:56
  • okey look [this question](http://stackoverflow.com/questions/25937298/ruby-on-rails-global-activerecordenum/25937972#25937972) – Roman Kiselenko Nov 14 '14 at 18:57
  • @Зелёный, could you please tell if I use enum - what is the best way to store additional info about elements like rules, calc methods, etc – Aleksandr K. Nov 14 '14 at 23:01

1 Answers1

0

Anytime you have "predefined" objects you should probably look into using an enum, though I don't really know why you would want to do that with an AR model. The whole idea behind an AR model is that it's an interactive object backed by a database that can have its attributes manipulated via CRUD operations.

Community
  • 1
  • 1
mattforni
  • 855
  • 5
  • 11
  • Is there any way to define enum somewhere global and use it as an object value? I've used enum for visibility field, but I defined it in model module like enum `visibility: [:public_tr, :unlisted_tr, :private_tr]` – Aleksandr K. Nov 14 '14 at 18:41
  • In your constants.rb file, but I don't believe AR is fully loaded and functional at that point in time. It seems like you don't actually want an AR model ... – mattforni Nov 14 '14 at 21:52
  • Yep, I don't know. Reasons for AR - use in relations, storing additional information about discipline like rules calc methods etc. Reasons for enum - simplicity, be on rails way, auto generated methods for check, get, set, compare – Aleksandr K. Nov 14 '14 at 22:59