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?
ruby-on-rails model
add a comment |
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?
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 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 at 9:47
add a comment |
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?
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 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 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 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 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 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
add a comment |
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
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 at 9:39
add a comment |
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
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
add a comment |
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
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 at 9:39
add a comment |
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
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 at 9:39
add a comment |
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
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 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 argumentid
, so I usedestroy_all
instead.
– weird honey
Nov 21 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 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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 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
add a comment |
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
add a comment |
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 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