Shared attribute accros models Enumerize
I'm using Enumerize gem and I would like to create an Enum that is shared among two models.
My model with Enum looks like this:
class StudyTypeEnum < ApplicationRecord
extends Enumerize
enumerize :studytype, in: {:full_time, :part_time}
end
and then I just include it in the other models
class Course < ApplicationRecord
include StudyTypeEnum
...
Im not sure how to create the migration now, do I need to create the StudyType columns in both StudyTypeEnum and Course models?
ruby-on-rails rubygems
add a comment |
I'm using Enumerize gem and I would like to create an Enum that is shared among two models.
My model with Enum looks like this:
class StudyTypeEnum < ApplicationRecord
extends Enumerize
enumerize :studytype, in: {:full_time, :part_time}
end
and then I just include it in the other models
class Course < ApplicationRecord
include StudyTypeEnum
...
Im not sure how to create the migration now, do I need to create the StudyType columns in both StudyTypeEnum and Course models?
ruby-on-rails rubygems
add a comment |
I'm using Enumerize gem and I would like to create an Enum that is shared among two models.
My model with Enum looks like this:
class StudyTypeEnum < ApplicationRecord
extends Enumerize
enumerize :studytype, in: {:full_time, :part_time}
end
and then I just include it in the other models
class Course < ApplicationRecord
include StudyTypeEnum
...
Im not sure how to create the migration now, do I need to create the StudyType columns in both StudyTypeEnum and Course models?
ruby-on-rails rubygems
I'm using Enumerize gem and I would like to create an Enum that is shared among two models.
My model with Enum looks like this:
class StudyTypeEnum < ApplicationRecord
extends Enumerize
enumerize :studytype, in: {:full_time, :part_time}
end
and then I just include it in the other models
class Course < ApplicationRecord
include StudyTypeEnum
...
Im not sure how to create the migration now, do I need to create the StudyType columns in both StudyTypeEnum and Course models?
ruby-on-rails rubygems
ruby-on-rails rubygems
asked Nov 16 '18 at 9:54
SpasitelSpasitel
937
937
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I would user concern for that kind of behavior.
in the file: app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
and then if any of your models need that field just do:
e.g in the file: app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
add a comment |
In Ruby classes cannot be included in other classes - only modules can. Classes can only be used for "classical" vertical inheritance (a class inherits from a single base class).
module StudyTypeEnum
def self.included(base)
base.extend Enumerize
base.class_eval do
enumerize :studytype, in: {:full_time, :part_time}
end
end
end
Since the code needs to be executed in the context of the class that includes the module we need define a self.included
method that is called when we include the class. You can also use ActiveSupport::Concern
which wraps this common Ruby idiom.
class Course < ApplicationRecord
include StudyTypeEnum
end
Im not sure how to create the migration now, do I need to create the
StudyType columns in both StudyTypeEnum and Course models?
Since StudyTypeEnum is a module that is mixed into model classes it does have a table.
You only need to add the column to the actual models - in this case Course - and any other model that you plan on including StudyTypeEnum in.
I'm not sure if I would use theEnumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.
– max
Nov 16 '18 at 14:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335330%2fshared-attribute-accros-models-enumerize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would user concern for that kind of behavior.
in the file: app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
and then if any of your models need that field just do:
e.g in the file: app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
add a comment |
I would user concern for that kind of behavior.
in the file: app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
and then if any of your models need that field just do:
e.g in the file: app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
add a comment |
I would user concern for that kind of behavior.
in the file: app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
and then if any of your models need that field just do:
e.g in the file: app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
I would user concern for that kind of behavior.
in the file: app/models/concerns/enumerable_study.rb
module EnumerableStudy
extend ActiveSupport::Concern
extends Enumerize
included do
enumerize :studytype, in: {:full_time, :part_time}
end
end
and then if any of your models need that field just do:
e.g in the file: app/models/course
class Course < ApplicationRecord
include EnumerableStudy
end
answered Nov 16 '18 at 14:03
Ignacy KasperowiczIgnacy Kasperowicz
378110
378110
add a comment |
add a comment |
In Ruby classes cannot be included in other classes - only modules can. Classes can only be used for "classical" vertical inheritance (a class inherits from a single base class).
module StudyTypeEnum
def self.included(base)
base.extend Enumerize
base.class_eval do
enumerize :studytype, in: {:full_time, :part_time}
end
end
end
Since the code needs to be executed in the context of the class that includes the module we need define a self.included
method that is called when we include the class. You can also use ActiveSupport::Concern
which wraps this common Ruby idiom.
class Course < ApplicationRecord
include StudyTypeEnum
end
Im not sure how to create the migration now, do I need to create the
StudyType columns in both StudyTypeEnum and Course models?
Since StudyTypeEnum is a module that is mixed into model classes it does have a table.
You only need to add the column to the actual models - in this case Course - and any other model that you plan on including StudyTypeEnum in.
I'm not sure if I would use theEnumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.
– max
Nov 16 '18 at 14:35
add a comment |
In Ruby classes cannot be included in other classes - only modules can. Classes can only be used for "classical" vertical inheritance (a class inherits from a single base class).
module StudyTypeEnum
def self.included(base)
base.extend Enumerize
base.class_eval do
enumerize :studytype, in: {:full_time, :part_time}
end
end
end
Since the code needs to be executed in the context of the class that includes the module we need define a self.included
method that is called when we include the class. You can also use ActiveSupport::Concern
which wraps this common Ruby idiom.
class Course < ApplicationRecord
include StudyTypeEnum
end
Im not sure how to create the migration now, do I need to create the
StudyType columns in both StudyTypeEnum and Course models?
Since StudyTypeEnum is a module that is mixed into model classes it does have a table.
You only need to add the column to the actual models - in this case Course - and any other model that you plan on including StudyTypeEnum in.
I'm not sure if I would use theEnumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.
– max
Nov 16 '18 at 14:35
add a comment |
In Ruby classes cannot be included in other classes - only modules can. Classes can only be used for "classical" vertical inheritance (a class inherits from a single base class).
module StudyTypeEnum
def self.included(base)
base.extend Enumerize
base.class_eval do
enumerize :studytype, in: {:full_time, :part_time}
end
end
end
Since the code needs to be executed in the context of the class that includes the module we need define a self.included
method that is called when we include the class. You can also use ActiveSupport::Concern
which wraps this common Ruby idiom.
class Course < ApplicationRecord
include StudyTypeEnum
end
Im not sure how to create the migration now, do I need to create the
StudyType columns in both StudyTypeEnum and Course models?
Since StudyTypeEnum is a module that is mixed into model classes it does have a table.
You only need to add the column to the actual models - in this case Course - and any other model that you plan on including StudyTypeEnum in.
In Ruby classes cannot be included in other classes - only modules can. Classes can only be used for "classical" vertical inheritance (a class inherits from a single base class).
module StudyTypeEnum
def self.included(base)
base.extend Enumerize
base.class_eval do
enumerize :studytype, in: {:full_time, :part_time}
end
end
end
Since the code needs to be executed in the context of the class that includes the module we need define a self.included
method that is called when we include the class. You can also use ActiveSupport::Concern
which wraps this common Ruby idiom.
class Course < ApplicationRecord
include StudyTypeEnum
end
Im not sure how to create the migration now, do I need to create the
StudyType columns in both StudyTypeEnum and Course models?
Since StudyTypeEnum is a module that is mixed into model classes it does have a table.
You only need to add the column to the actual models - in this case Course - and any other model that you plan on including StudyTypeEnum in.
answered Nov 16 '18 at 14:32
maxmax
46.6k1060105
46.6k1060105
I'm not sure if I would use theEnumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.
– max
Nov 16 '18 at 14:35
add a comment |
I'm not sure if I would use theEnumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.
– max
Nov 16 '18 at 14:35
I'm not sure if I would use the
Enumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.– max
Nov 16 '18 at 14:35
I'm not sure if I would use the
Enumerize
gem though. It stores the enums as strings instead of simple integers like ActiveRecord::Enum which are faster to index and compare. I would say that the gem author kind of missed the whole point.– max
Nov 16 '18 at 14:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335330%2fshared-attribute-accros-models-enumerize%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown