使用带有关联的Rails默认范围后无法删除记录(Cannot delete record after using Rails default scope with associations)

编程入门 行业动态 更新时间:2024-10-11 19:15:59
使用带有关联的Rails默认范围后无法删除记录(Cannot delete record after using Rails default scope with associations)

我有表emails和users 。 emails表有两个字段: user_id和publish_id 。

我使用了这样的默认范围:

default_scope joins('LEFT OUTER JOIN users ON users.id = emails.publish_id').where("users.status IN (?)", ["approve", "spam"])`

由于users表包含publishers和subscribers ,因此我使用users_id与subscribers关联,并使用publish_id与publishers subscribers关联。

我正在使用以下内容获取记录:

email = Email.unscoped.find(:id) email.delete

但是,我收到以下错误:

有人可以告诉我如何删除这个或如何强制删除删除电子邮件?

完整的错误消息: -

em=Email.unscoped.last Email Load (0.5ms) SELECT `emails`.* FROM `emails` ORDER BY `emails`.`id` DESC LIMIT 1 => #<Email id: 4, user_id: 2, body: "asdasd", complete_email: {}, created_at: "2013-08-28 15:29:10", updated_at: "2013-08-28 15:29:10", snapshot_status: nil, thumbnail: nil, deleted_at: nil, read: nil, publish_id: 1, email_type: nil, snapshot_thumb_file_name: nil, snapshot_thumb_content_type: nil, snapshot_thumb_file_size: nil, snapshot_thumb_updated_at: nil> 1.9.3p448 :017 > em.delete SQL (0.5ms) UPDATE `emails` SET `deleted_at` = '2013-08-28 15:29:24' WHERE `emails`.`id` = 4 AND (`emails`.`deleted_at` IS NULL) AND (users.status IN ('approve','spam')) ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'users.status' in 'where clause': UPDATE `emails` SET `deleted_at` = '2013-08-28 15:29:24' WHERE `emails`.`id` = 4 AND (`emails`.`deleted_at` IS NULL) AND (users.status IN ('approve','spam')) from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:238:in `exec_delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:96:in `update' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:294:in `update_all' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/acts_as_paranoid-0.4.2/lib/acts_as_paranoid/relation.rb:24:in `delete_all' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:442:in `delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/querying.rb:7:in `delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/persistence.rb:119:in `delete' from (irb):17 from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require'

以下是User类定义: -

User => User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, provider: string, uid: string, name: string, username: string, role: string, photo_file_name: string, photo_content_type: string, photo_file_size: integer, photo_updated_at: datetime, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string, tag_listing: string, status: string, private_publisher: boolean, public_wall_display: boolean, personal_email: string)

I have tables emails and users. The emails table has two fields: user_id and publish_id.

I have used a default scope like this:

default_scope joins('LEFT OUTER JOIN users ON users.id = emails.publish_id').where("users.status IN (?)", ["approve", "spam"])`

As the users table holds publishers and subscribers, so I have used users_id to associate with subscribers and publish_id to associate with publishers.

I am fetching a record using:

email = Email.unscoped.find(:id) email.delete

But, I get the following error:

Can someone tell me how can I remove this or how can I force remove delete the email?

Complete error message :-

em=Email.unscoped.last Email Load (0.5ms) SELECT `emails`.* FROM `emails` ORDER BY `emails`.`id` DESC LIMIT 1 => #<Email id: 4, user_id: 2, body: "asdasd", complete_email: {}, created_at: "2013-08-28 15:29:10", updated_at: "2013-08-28 15:29:10", snapshot_status: nil, thumbnail: nil, deleted_at: nil, read: nil, publish_id: 1, email_type: nil, snapshot_thumb_file_name: nil, snapshot_thumb_content_type: nil, snapshot_thumb_file_size: nil, snapshot_thumb_updated_at: nil> 1.9.3p448 :017 > em.delete SQL (0.5ms) UPDATE `emails` SET `deleted_at` = '2013-08-28 15:29:24' WHERE `emails`.`id` = 4 AND (`emails`.`deleted_at` IS NULL) AND (users.status IN ('approve','spam')) ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'users.status' in 'where clause': UPDATE `emails` SET `deleted_at` = '2013-08-28 15:29:24' WHERE `emails`.`id` = 4 AND (`emails`.`deleted_at` IS NULL) AND (users.status IN ('approve','spam')) from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:238:in `exec_delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:96:in `update' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:294:in `update_all' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/acts_as_paranoid-0.4.2/lib/acts_as_paranoid/relation.rb:24:in `delete_all' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:442:in `delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/querying.rb:7:in `delete' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/persistence.rb:119:in `delete' from (irb):17 from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start' from /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require'

Following is the User class definition :-

User => User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, provider: string, uid: string, name: string, username: string, role: string, photo_file_name: string, photo_content_type: string, photo_file_size: integer, photo_updated_at: datetime, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string, tag_listing: string, status: string, private_publisher: boolean, public_wall_display: boolean, personal_email: string)

最满意答案

你应该使用像这样的unscoped块 :

Email.unscoped do email = Email.find(id) email.delete end

或(相同,但更短):

Email.unscoped do Email.find(id).delete end

或者在一个查询而不是两个查询中执行此操作:

Email.unscoped do Email.where(:id => id).delete_all end

You should use an unscoped block like this:

Email.unscoped do email = Email.find(id) email.delete end

or (same, but shorter):

Email.unscoped do Email.find(id).delete end

or to do it in one query instead of two:

Email.unscoped do Email.where(:id => id).delete_all end

更多推荐

本文发布于:2023-08-08 00:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:record   delete   Rails   associations   scope

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!