删除没有关联任务的帖子

时间:2017-11-03 11:22:42

标签: sql ruby-on-rails ruby activerecord

我希望能够在没有相关任务的情况下销毁我的帖子。但我目前正面临一个SQL错误:

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);

        **//always gives true value here...**
        Log.e("loginStatus", pref.getBoolean("activity_executed", false) + "");
        if (pref.getBoolean("activity_executed", false)) {
            Log.e("loginStatus", pref.getBoolean("activity_executed", false) + "");

            Intent intent = new Intent(this, LiveTrack.class);
            startActivity(intent);
            finish();
        } else {
            Log.e("loginStatus", "notlogin");
        }
    }
}

经过几次搜索,我看到它来自协会和外键。但我目前无法解决问题。

我已经测试过将(可选:true)放入我的模型中。

我也尝试将外键更改为(,on_delete :: cascade)& (,on_delete :: nullify)但它仍然无法正常工作。

我的代码=

ActiveRecord::InvalidForeignKey (SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "posts" WHERE "posts"."id" = ?):

要毁灭:

//Post Model
class Post < ApplicationRecord
  has_one :task
end

//Task Model
class Task < ApplicationRecord
  belongs_to :post, optional: true
end

迁移文件:(也尝试使用on_delete :: nullify)

//Destroy_one into the Post Controller
def destoy_one
  @post.destroy
end

你还有其他解决办法吗?

2 个答案:

答案 0 :(得分:1)

我通过在帖子

中实现deleted_state来解决这个问题
def destroy_one
  @post.update(deleted_state: true)
end

之后可以将默认范围放入Post模型中,如下所示:

default_scope { where(deleted_state: false) }

像这样一切都会顺利运作!!

答案 1 :(得分:0)

您可以对<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel ="stylesheet" href="style_wbr.css"/> <title>PORTAIL</title> </head> <body> <h1 id ="titre">Reporting</h1> <img id="image" src="http://www.tessares.net/wp-content/uploads/2016/06/sa.png" > <form method="post" action="/authentification.php"> <div class="quit"> <input type="submit" name="quit" id="quit" value="Quitter"/> </div> <div> <input type="button" id ="historique" name="historique" value="Historique"/> </div> </form> <br></br> <br></br> <?php if(isset($_POST['historique'])) { $rqt = "SELECT TOP (10) DEP_Operation.id, DEP_Operation.operationDate, DEP_Users.fullName FROM DEP_Operation INNER JOIN DEP_Products ON DEP_Operation.productID = DEP_Products.id INNER JOIN DEP_Errors ON DEP_Operation.errorID = DEP_Errors.id INNER JOIN DEP_Users ON DEP_Operation.personInCharge = DEP_Users.id"; $stmt = mssql_query($rqt); ?> <table id="tab" border="1"> <tr> <th>id</th> <th>Date</th> <th>Dépanneur</th> </tr> <?php while ($data = mssql_fetch_assoc($stmt)) { ?> <tr> <td><?php echo utf8_encode ($data['id']);?></td> <td><?php echo utf8_encode ($data['operationDate']);?></td> <td><?php echo utf8_encode ($data['fullName']);?></td> </tr> <?php } ?> </table> <?php } ?> </body> </html> 的销毁操作使用回调作为变体:

User