I am doing some saving and destroying operation on a object which behaves weird.
if already_in_server
bookmark_obj = array_already_bookmarked.detect {|bookmark| bookmark.article_doi == article_doi["doi"]}
if 1 == article_doi["isbookmarked"]
binding.pry
bookmark_obj.updated_at = Time.now
bookmark_obj.save
else
bookmark_obj.destroy
end
end
Association between them:
up has_many -----> bookmarks
bookmark belongs_to --> up
But when I try to call up.bookmarks, It also returns destroyed objects and
bookmark_obj.updated_at = Time.now
is not updated.
Use Reload which will reloads the record from the database. so after you save record you can do:
if already_in_server
bookmark_obj = array_already_bookmarked.detect {|bookmark| bookmark.article_doi == article_doi["doi"]}
if 1 == article_doi["isbookmarked"]
binding.pry
bookmark_obj.updated_at = Time.now
bookmark_obj.save
bookmark_obj.reload # Add this line
else
bookmark_obj.destroy
end
end
and in case of deleted then you will need to reload the other relation, as you mentioned up
has_many bookmarks
bookmark_obj.destroy
up.bookmarks # this one will contain the destroyed bookmark
up.reload # reload to retrieve fresh list
up.bookmarks # will not contain the destroyed bookmark