rails delete old record while creating new record
here is my condition: i have a model named Game
and a model named GameLevel
, their relationship is like code below:
class Game < ApplicationRecord
has_many :game_level
class GameLevel < ApplicationRecord
belongs_to :game
here is my situation:
when a Game
has a GameLevel
which name is default
, it should not have low
, medium
, high
;
when a Game
has a GameLevel
which names are low
, medium
, high
, it should not have default
.
so how do i delete default
when i creating(or updating when record exists) low, medium, high
, and how do i delete low ,medium, high
when i creating(or updating when record exists) default
in rails model?
ruby-on-rails model
add a comment |
here is my condition: i have a model named Game
and a model named GameLevel
, their relationship is like code below:
class Game < ApplicationRecord
has_many :game_level
class GameLevel < ApplicationRecord
belongs_to :game
here is my situation:
when a Game
has a GameLevel
which name is default
, it should not have low
, medium
, high
;
when a Game
has a GameLevel
which names are low
, medium
, high
, it should not have default
.
so how do i delete default
when i creating(or updating when record exists) low, medium, high
, and how do i delete low ,medium, high
when i creating(or updating when record exists) default
in rails model?
ruby-on-rails model
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
@devanand my solution is that define a constant array in classGameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.
– weird honey
Nov 21 '18 at 9:47
add a comment |
here is my condition: i have a model named Game
and a model named GameLevel
, their relationship is like code below:
class Game < ApplicationRecord
has_many :game_level
class GameLevel < ApplicationRecord
belongs_to :game
here is my situation:
when a Game
has a GameLevel
which name is default
, it should not have low
, medium
, high
;
when a Game
has a GameLevel
which names are low
, medium
, high
, it should not have default
.
so how do i delete default
when i creating(or updating when record exists) low, medium, high
, and how do i delete low ,medium, high
when i creating(or updating when record exists) default
in rails model?
ruby-on-rails model
here is my condition: i have a model named Game
and a model named GameLevel
, their relationship is like code below:
class Game < ApplicationRecord
has_many :game_level
class GameLevel < ApplicationRecord
belongs_to :game
here is my situation:
when a Game
has a GameLevel
which name is default
, it should not have low
, medium
, high
;
when a Game
has a GameLevel
which names are low
, medium
, high
, it should not have default
.
so how do i delete default
when i creating(or updating when record exists) low, medium, high
, and how do i delete low ,medium, high
when i creating(or updating when record exists) default
in rails model?
ruby-on-rails model
ruby-on-rails model
asked Nov 21 '18 at 8:25
weird honeyweird honey
17310
17310
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
@devanand my solution is that define a constant array in classGameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.
– weird honey
Nov 21 '18 at 9:47
add a comment |
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
@devanand my solution is that define a constant array in classGameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.
– weird honey
Nov 21 '18 at 9:47
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
@devanand my solution is that define a constant array in class
GameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.– weird honey
Nov 21 '18 at 9:47
@devanand my solution is that define a constant array in class
GameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.– weird honey
Nov 21 '18 at 9:47
add a comment |
2 Answers
2
active
oldest
votes
You can use before_save
callback mentioned here
And you should use game_levels
instead of game_level
as convention.
As an example you can write something like this;
class GameLevel < ApplicationRecord #or ActiveRecord::Base
belongs_to :game
before_save :remove_unnecessary
private
def remove_unnecessary
if self.name == "default"
self.game.game_levels.where(name: ["low", "medium", "high"]).destroy_all()
end
if ["low", "medium", "high"].any?{ |type| self.name == type }
self.game.game_levels.where(name: "default").destroy_all()
end
end
end
for destroy you can look to here
1
great answer! I used your method and problem solved perfectly! ps:destroy
requires 1 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 '18 at 9:39
add a comment |
You can use below code
class GmaeLevel < ApplicationRecord
belongs_to :game
before_save :update_date
private
def update_date
self.name == "default" ? self.game.game_levels.(name: ["low","medium","high"]).destroy_all : self.game.game_levels.where(name: "default").destroy
end
end
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
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%2f53407872%2frails-delete-old-record-while-creating-new-record%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
You can use before_save
callback mentioned here
And you should use game_levels
instead of game_level
as convention.
As an example you can write something like this;
class GameLevel < ApplicationRecord #or ActiveRecord::Base
belongs_to :game
before_save :remove_unnecessary
private
def remove_unnecessary
if self.name == "default"
self.game.game_levels.where(name: ["low", "medium", "high"]).destroy_all()
end
if ["low", "medium", "high"].any?{ |type| self.name == type }
self.game.game_levels.where(name: "default").destroy_all()
end
end
end
for destroy you can look to here
1
great answer! I used your method and problem solved perfectly! ps:destroy
requires 1 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 '18 at 9:39
add a comment |
You can use before_save
callback mentioned here
And you should use game_levels
instead of game_level
as convention.
As an example you can write something like this;
class GameLevel < ApplicationRecord #or ActiveRecord::Base
belongs_to :game
before_save :remove_unnecessary
private
def remove_unnecessary
if self.name == "default"
self.game.game_levels.where(name: ["low", "medium", "high"]).destroy_all()
end
if ["low", "medium", "high"].any?{ |type| self.name == type }
self.game.game_levels.where(name: "default").destroy_all()
end
end
end
for destroy you can look to here
1
great answer! I used your method and problem solved perfectly! ps:destroy
requires 1 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 '18 at 9:39
add a comment |
You can use before_save
callback mentioned here
And you should use game_levels
instead of game_level
as convention.
As an example you can write something like this;
class GameLevel < ApplicationRecord #or ActiveRecord::Base
belongs_to :game
before_save :remove_unnecessary
private
def remove_unnecessary
if self.name == "default"
self.game.game_levels.where(name: ["low", "medium", "high"]).destroy_all()
end
if ["low", "medium", "high"].any?{ |type| self.name == type }
self.game.game_levels.where(name: "default").destroy_all()
end
end
end
for destroy you can look to here
You can use before_save
callback mentioned here
And you should use game_levels
instead of game_level
as convention.
As an example you can write something like this;
class GameLevel < ApplicationRecord #or ActiveRecord::Base
belongs_to :game
before_save :remove_unnecessary
private
def remove_unnecessary
if self.name == "default"
self.game.game_levels.where(name: ["low", "medium", "high"]).destroy_all()
end
if ["low", "medium", "high"].any?{ |type| self.name == type }
self.game.game_levels.where(name: "default").destroy_all()
end
end
end
for destroy you can look to here
edited Nov 21 '18 at 10:29
answered Nov 21 '18 at 8:56
Onur Eren ElibolOnur Eren Elibol
1711110
1711110
1
great answer! I used your method and problem solved perfectly! ps:destroy
requires 1 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 '18 at 9:39
add a comment |
1
great answer! I used your method and problem solved perfectly! ps:destroy
requires 1 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 '18 at 9:39
1
1
great answer! I used your method and problem solved perfectly! ps:
destroy
requires 1 argument id
, so I use destroy_all
instead.– weird honey
Nov 21 '18 at 9:39
great answer! I used your method and problem solved perfectly! ps:
destroy
requires 1 argument id
, so I use destroy_all
instead.– weird honey
Nov 21 '18 at 9:39
add a comment |
You can use below code
class GmaeLevel < ApplicationRecord
belongs_to :game
before_save :update_date
private
def update_date
self.name == "default" ? self.game.game_levels.(name: ["low","medium","high"]).destroy_all : self.game.game_levels.where(name: "default").destroy
end
end
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
add a comment |
You can use below code
class GmaeLevel < ApplicationRecord
belongs_to :game
before_save :update_date
private
def update_date
self.name == "default" ? self.game.game_levels.(name: ["low","medium","high"]).destroy_all : self.game.game_levels.where(name: "default").destroy
end
end
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
add a comment |
You can use below code
class GmaeLevel < ApplicationRecord
belongs_to :game
before_save :update_date
private
def update_date
self.name == "default" ? self.game.game_levels.(name: ["low","medium","high"]).destroy_all : self.game.game_levels.where(name: "default").destroy
end
end
You can use below code
class GmaeLevel < ApplicationRecord
belongs_to :game
before_save :update_date
private
def update_date
self.name == "default" ? self.game.game_levels.(name: ["low","medium","high"]).destroy_all : self.game.game_levels.where(name: "default").destroy
end
end
answered Nov 21 '18 at 9:53
user3678149user3678149
1676
1676
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
add a comment |
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 '18 at 10:03
All the best :)
– user3678149
Nov 21 '18 at 10:12
All the best :)
– user3678149
Nov 21 '18 at 10:12
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53407872%2frails-delete-old-record-while-creating-new-record%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
if there is a solution what properly is, there are still some questions like are more "same levels" allowed and how do i handle this case if not.
– devanand
Nov 21 '18 at 9:43
@devanand my solution is that define a constant array in class
GameLevel
, if more "same levels" is required, add an element to the constant, because "default" level is against all other levels. Any better suggestions from you would be appreciate.– weird honey
Nov 21 '18 at 9:47