rails delete old record while creating new record











up vote
0
down vote

favorite












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?










share|improve this question






















  • 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 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 at 9:47















up vote
0
down vote

favorite












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?










share|improve this question






















  • 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 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 at 9:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 8:25









weird honey

17410




17410












  • 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 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 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 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 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 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 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 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 at 9:47












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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






share|improve this answer



















  • 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 at 9:39




















up vote
1
down vote













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





share|improve this answer





















  • thanks for ur solution, it works as good as the above one and looks more succinct.
    – weird honey
    Nov 21 at 10:03










  • All the best :)
    – user3678149
    Nov 21 at 10:12











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',
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
});


}
});














 

draft saved


draft discarded


















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








up vote
1
down vote



accepted










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






share|improve this answer



















  • 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 at 9:39

















up vote
1
down vote



accepted










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






share|improve this answer



















  • 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 at 9:39















up vote
1
down vote



accepted







up vote
1
down vote



accepted






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 10:29

























answered Nov 21 at 8:56









Onur Eren Elibol

1711110




1711110








  • 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 at 9:39
















  • 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 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 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 at 9:39














up vote
1
down vote













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





share|improve this answer





















  • thanks for ur solution, it works as good as the above one and looks more succinct.
    – weird honey
    Nov 21 at 10:03










  • All the best :)
    – user3678149
    Nov 21 at 10:12















up vote
1
down vote













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





share|improve this answer





















  • thanks for ur solution, it works as good as the above one and looks more succinct.
    – weird honey
    Nov 21 at 10:03










  • All the best :)
    – user3678149
    Nov 21 at 10:12













up vote
1
down vote










up vote
1
down vote









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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 9:53









user3678149

1166




1166












  • thanks for ur solution, it works as good as the above one and looks more succinct.
    – weird honey
    Nov 21 at 10:03










  • All the best :)
    – user3678149
    Nov 21 at 10:12


















  • thanks for ur solution, it works as good as the above one and looks more succinct.
    – weird honey
    Nov 21 at 10:03










  • All the best :)
    – user3678149
    Nov 21 at 10:12
















thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 at 10:03




thanks for ur solution, it works as good as the above one and looks more succinct.
– weird honey
Nov 21 at 10:03












All the best :)
– user3678149
Nov 21 at 10:12




All the best :)
– user3678149
Nov 21 at 10:12


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...