Rails破坏了破坏错误记录的行为

时间:2014-08-04 07:08:04

标签: ruby-on-rails heroku controller destroy

我发布了一个类似的问题earlier,我认为我已经解决了这个问题,但我错了。我认为活动没有被删除,但事实证明它正在删除活动,这只是错误的活动。我的任何其他型号都没有这个问题,这只发生在我的某个应用上。我的分叉应用程序具有完全相同的代码正常工作。我不知道为什么会这样。

projects_controller.rb

class ProjectsController < ApplicationController

    before_filter :find_project, only: [:edit, :update, :destroy]

    def create
        params[:project][:about] = sanitize_redactor(params[:project][:about])
        @project = current_member.projects.new(params[:project])

        respond_to do |format|
          if @project.save
            current_member.create_activity(@project, 'created')
            format.html { redirect_to @project }
            format.json { render json: @project, status: :created, location: @project }
          else
            format.html { render action: "new" }
            format.json { render json: @project.errors, alert: 'Please make sure all required fields are filled in and all fields are formatted correctly.' }
          end
       end
    end

    def destroy
        @project = Project.find(params[:id])
        @activity = Activity.find_by_targetable_id(params[:id])
        if @activity
            @activity.destroy
        end
        @project.destroy

        respond_to do |format|
          format.html { redirect_to profile_projects_path(current_member) }
          format.json { head :no_content }
          format.js
        end
    end

    def find_project
        @project = current_member.projects.find(params[:id])
    end 

end

member.rb

class Member < ActiveRecord::Base

    def create_activity(item, action)
        activity = activities.new
        activity.targetable = item
        activity.action = action 
        activity.save 
        activity
    end

end

移植

class CreateActivities < ActiveRecord::Migration
    def change
        create_table :activities do |t|
            t.integer :member_id
            t.string :action

            t.integer :targetable_id
            t.string :targetable_type

            t.timestamps
        end

        add_index :activities, :member_id
        add_index :activities, [:targetable_id, :targetable_type]

    end
end

**** **** EDIT

activity.rb

class Activity < ActiveRecord::Base
    belongs_to :member
    belongs_to :targetable, polymorphic: true
    acts_as_votable

    self.per_page = 36

    def self.for_member(member, options={})
        options[:page] ||= 1
        following_ids = member.following_members.map(&:id).push(member.id)
        where("member_id in (?)", following_ids).
            order("created_at desc").
            page(options[:page])
    end 

end

project.rb

class Project < ActiveRecord::Base
    belongs_to :member

    attr_accessible :about, :blurb, :category, :markers, :video, :website, :name, :avatar, :banner, :marker_list, :city

    acts_as_votable
    acts_as_followable
    acts_as_ordered_taggable
    acts_as_ordered_taggable_on :markers
    acts_as_messageable
    has_many :comments, as: :commentable, :dependent => :destroy
    has_many :uploads, as: :uploadable, :dependent => :destroy
    has_many :updates, as: :updateable, :dependent => :destroy

    def to_param
        "#{id}-#{name.parameterize}"
    end  

end

member.rb

class Member < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable

    # Setup accessible (or protected) attributes for your model
    attr_accessible :email, :email_confirmation, :password, :password_confirmation, :remember_me,
        :full_name, :user_name, :pursuits, :avatar, :bio, :city, :state, :country, :pursuit_list, 
        :facebook, :twitter, :linkedin, :soundcloud, :youtube, :vimeo, :instagram, :flickr, :google, :pinterest, :blog, :website, :banner

    has_many :medium, :dependent => :destroy
    has_many :projects, :dependent => :destroy
    has_many :events, :dependent => :destroy
    has_many :statuses, :dependent => :destroy
    has_many :activities, :dependent => :destroy
    has_many :listings, :dependent => :destroy
    has_many :comments, :dependent => :destroy
    has_many :uploads, :dependent => :destroy
    has_many :updates, :dependent => :destroy
    has_many :assets, :dependent => :destroy
    acts_as_follower
    acts_as_followable
    acts_as_ordered_taggable
    acts_as_ordered_taggable_on :pursuits
    acts_as_voter
    acts_as_messageable

    def to_param
        user_name
    end 

    def name
        user_name
    end 

    def mailboxer_email(object)
        return user_name
    end

    def create_activity(item, action)
        activity = activities.new
        activity.targetable = item
        activity.action = action 
        activity.save 
        activity
    end

end

1 个答案:

答案 0 :(得分:1)

...有趣

-

活动模型

你提到的Activity模型并没有正确销毁。考虑到这一点,您将要查看有助于销毁机制的所有步骤:

def destroy
   ...
   @activity = Activity.find_by_targetable_id(params[:id])
   @activity.destroy if @activity

首先要做的是什么 - targetable_id是什么?此外,如果您正在使用Rails 4,那么您将能够使用find_by方法和属性哈希:

@activity = Activity.find_by targetable_id: params[:id]

这可能是导致此问题的主要原因 - 上述代码基本上会查找Activitytargetable_id个记录,其中id个参数与您从请求中传递的参数相同polymorphic association

我不是一个博彩人,但我猜测这就是问题所在


Polymorphic

好的,我发现了问题。

您试图在没有正确引用的情况下调用ActiveRecord association。多态关联允许您为多个模型创建______type,将关联数据存储在单个表/模型中:

enter image description here

请注意上面的示例(作为您的代码)是如何包含activity_id列的?这对您的解决方案来说是很重要的 - 因为存储模型的是您保存的ID:

  

当您在 ActiveRecord关联中保存数据时,它会使用名为foreign_key的内容来定义模型中的关联数据。正如您所知,此外键通常类似_type或类似内容。

     

不同之处在于您的关联是多态关联,这意味着您可以在一个表中存储多个模型类型。这意味着ActiveRecord / Rails必须引用您正在保存的模型,这是#app/models/project.rb Class Project < ActiveRecord::Base has_many :activities, as: :targetable, dependent: :destroy end #app/models/activity.rb Class Activity < ActiveRecord::Base belongs_to :targetable, polymorphic: true end 列的来源

我的理论是,您的本地数据库将保存在您的&#34;多态&#34;数据表 - 意思是当您查找ID时,它会带回一个不是正确模型的记录


<强>修正

我会推荐以下内容:

#app/controllers/projects_controller.rb
Class ProjectController < ApplicationController
   def destroy
      @project = Project.find params[:id] # should find project.activities too
      @project.destroy
   end
end

我不知道你的模特联想,所以我只是做了上面的事情。如果您按上述方式设置关联,则可以调用以下内容:

Project

我使用以上几个重要原因:

  
      
  1. 您为两个对象使用相同的ID(表示他们已关联)
  2.   
  3. 您可以使用dependent: :destroy方法
  4.   

最重要的是,您将能够仅销毁{{1}}对象,并将其家属也销毁,从而实现您期望的结果