Frage

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)
War es hilfreich?

Lösung

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

Andere Tipps

Try

email = Email.unscoped.reload.find(:id)
email.delete
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top