通过has_many删除记录:通过

时间:2012-09-10 05:40:05

标签: ruby-on-rails ruby ruby-on-rails-3 has-many-through has-many

我的模型架构:

用户

has_many :activities
has_many :companies through: :activities

公司

has_many :activities
has_many :users, through: :activities

活动

belongs_to :user
belongs_to :company

当我尝试运行current_user.activities.delete_all(["company_id=?", params[:id]])cleardestroy时出现问题我收到此错误:

ArgumentError: wrong number of arguments (1 for 0)
    from /home/rom/.rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:156:in `delete_all'
    from /home/rom/.rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:46:in `delete_all'

1 个答案:

答案 0 :(得分:1)

使用以下

Activity.delete_all(["company_id=? AND user_id=?", params[:id], current_user.id])