Cannot delete record after using Rails default scope with associations -


i have tables emails , users. emails table has 2 fields: user_id , publish_id.

i have used default scope this:

default_scope joins('left outer join users on users.id = emails.publish_id').where("users.status in (?)", ["approve", "spam"])` 

as users table holds publishers , subscribers, have used users_id associate subscribers , publish_id associate publishers.

i fetching record using:

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

but, following error:

can tell me how can remove or how can force remove delete email?

complete error message :-

 em=email.unscoped.last   email load (0.5ms)  select `emails`.* `emails` order `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' `emails`.`id` = 4 , (`emails`.`deleted_at` null) , (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' `emails`.`id` = 4 , (`emails`.`deleted_at` null) , (users.status in ('approve','spam'))     /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'     /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'     /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'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'     /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'     /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'     /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'     /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'     /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'     /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'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:294:in `update_all'     /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'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/relation.rb:442:in `delete'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/querying.rb:7:in `delete'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/activerecord-3.2.13/lib/active_record/persistence.rb:119:in `delete'     (irb):17     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'     /home/awsome/.rvm/gems/ruby-1.9.3-p448@awsome/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'     script/rails:6:in `require' 

following 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) 

you should use unscoped block this:

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

or (same, shorter):

email.unscoped   email.find(id).delete end 

or in 1 query instead of two:

email.unscoped   email.where(:id => id).delete_all end 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -