找不到ID为1的项目[WHERE“projects”。“deleted_at”IS NULL]

时间:2022-09-08 13:44:16

I am using the paranoia gem.

我正在使用偏执狂宝石。

I have a bunch of projects in my database but for somereason they are behaving as if they were deleted. When I navigate to my projects/1 route I get the error:

我的数据库中有很多项目,但是出于某种原因,它们的行为就好像被删除了一样。当我导航到我的项目/ 1路线时,我收到错误:

Couldn't find Project with id=1 [WHERE "projects"."deleted_at" IS NULL]

When I type this in my console:

当我在我的控制台中键入它时:

Project.find(1).deleted_at

I get

nil

What is happening here?

这里发生了什么?

This is my controller show action:

这是我的控制器显示动作:

def show
    @project = Project.find(params[:id])
    @comments = Comment.all.where(:project_id => @project.id)
    @updates = ProjectUpdate.all.where(:project_id => @project.id)
end

Error happens on

发生错误

@project = Project.find(params[:id])

Here are some model Project scopes:

以下是一些模型项目范围:

scope :by_category, lambda {|category| {:conditions => {:category_id => category.id}}}
scope :by_npo, lambda {|npo| {:conditions => {:npo_id => npo.id}}}

With Project.find(1) I get:

使用Project.find(1)我得到:

=> #<Project id: 1, name: "project 1", npo_id: 1, description: "project1 description", location_id:
4, singular_unit: "1", past_tense_action: "past tense action", conversion: #<BigDecimal:7547d78,'0.1
5E1',18(45)>, created_at: "2014-05-13 00:12:33", updated_at: "2014-05-22 01:20:51", future_tense_act
ion: "future tense action", plural_unit: "2", amount1: #<BigDecimal:75475b0,'0.1E2',9(36)>, amount2:
 #<BigDecimal:7547520,'0.2E2',9(36)>, amount3: #<BigDecimal:7547490,'0.3E2',9(36)>, min_amount: nil,
 other_amount: true, short_description: "project1 short description", featured_image_id: 3, deleted_
at: nil>

From my index page I link to it 2ice(this is the relevant code:

从我的索引页面我链接到它2ice(这是相关的代码:

    <div class="pj">

      <h5><%= link_to project.name, project, :class => "button-link" %> </h5>
      <hr />
      <div class="index_featured_image">
        <%= link_to image_tag(project.get_featured_image, :alt => "Project Title", class: "featured_image"), project %>
      </div>
      <div class="proj-content">
        <p><strong><%= link_to project.npo.name, npo_path(project.npo) %></strong></br>
        <%= project.short_description %></p>
      </div>
      <div>
        <p class="project-loc"><i class="footicons fi-marker large"></i>  <%= project.location.city_state %></p>
      </div>
      <div class="text-center">
        <%= button_tag :type => "button", :class => "radius" do %>
          <%= link_to "View More", project, :class => "button-link" %>
        <% end %>
      </div>
    </div>

1 个解决方案

#1


0  

Couple guesses and some advice. First the guesses. If Project.find(params[:id]) calls WHERE "projects"."deleted_at" IS NULL condition on the table, there must be a default_scope call on the Project. Though it's still weird why id prevents the find method from finding the correct project, if it's really not deleted. Let me see that default_scope (or better just post the whole model in a gist) and I might tell a bit more.

夫妻猜测和一些建议。首先是猜测。如果Project.find(params [:id])在表上调用WHERE“projects”。“deleted_at”IS NULL条件,则Project上必须有一个default_scope调用。虽然它仍然很奇怪为什么id阻止find方法找到正确的项目,如果它真的没有被删除。让我看看default_scope(或者更好的只是将整个模型发布在一个要点中),我可能会告诉你更多。

Now some advice. It's not really related to the current issue, but I believe, it will make your Rails life a lot easier these days.

现在一些建议。它与当前的问题没有多大关系,但我相信,这些日子会让你的Rails生活变得更加容易。

  1. Try not to use deprecated syntax, get on with the new one. E.g. those scope calls should look like this

    尽量不要使用弃用的语法,继续使用新语法。例如。那些范围调用应该是这样的

    scope :by_category, ->(category){where(category_id: category.id)}
    
  2. Don't use .all with .where. You just don't need it. The .where call will return an ActiveRecord::Relation object you are looking for.

    不要将.all与.where一起使用。你只是不需要它。 .where调用将返回您正在寻找的ActiveRecord :: Relation对象。

  3. Try to reduce the number of instance variables in controllers. For example you can omit the use of the @comments and @updates variables in your show action, provided that you have set the needed associations on the Project model.

    尝试减少控制器中的实例变量数量。例如,您可以在show动作中省略@comments和@updates变量的使用,前提是您已在Project模型上设置了所需的关联。

    has_many :comments
    has_many :updates, class_name: ProjectUpdate
    

    Then in the view you can refer to them as @project.comments and @project.updates without the need to store them separately. It will clean up the controller a bit. Also, if you want to ensure that the comments and updates are not lazy-loaded, you can load them together with the project by calling @project = Project.includes(:comments, :updates).find(params[:id]).

    然后在视图中,您可以将它们称为@ project.comments和@project.updates,而无需单独存储它们。它会稍微清理一下控制器。此外,如果要确保注释和更新不是延迟加载的,可以通过调用@project = Project.includes(:comments,:updates).find(params [:id])将它们与项目一起加载。 。

Hope that would be helpful. I'll update the answer would I have something more concrete on the problem.

希望会有所帮助。我会更新答案,我会在问题上找到更具体的内容。

#1


0  

Couple guesses and some advice. First the guesses. If Project.find(params[:id]) calls WHERE "projects"."deleted_at" IS NULL condition on the table, there must be a default_scope call on the Project. Though it's still weird why id prevents the find method from finding the correct project, if it's really not deleted. Let me see that default_scope (or better just post the whole model in a gist) and I might tell a bit more.

夫妻猜测和一些建议。首先是猜测。如果Project.find(params [:id])在表上调用WHERE“projects”。“deleted_at”IS NULL条件,则Project上必须有一个default_scope调用。虽然它仍然很奇怪为什么id阻止find方法找到正确的项目,如果它真的没有被删除。让我看看default_scope(或者更好的只是将整个模型发布在一个要点中),我可能会告诉你更多。

Now some advice. It's not really related to the current issue, but I believe, it will make your Rails life a lot easier these days.

现在一些建议。它与当前的问题没有多大关系,但我相信,这些日子会让你的Rails生活变得更加容易。

  1. Try not to use deprecated syntax, get on with the new one. E.g. those scope calls should look like this

    尽量不要使用弃用的语法,继续使用新语法。例如。那些范围调用应该是这样的

    scope :by_category, ->(category){where(category_id: category.id)}
    
  2. Don't use .all with .where. You just don't need it. The .where call will return an ActiveRecord::Relation object you are looking for.

    不要将.all与.where一起使用。你只是不需要它。 .where调用将返回您正在寻找的ActiveRecord :: Relation对象。

  3. Try to reduce the number of instance variables in controllers. For example you can omit the use of the @comments and @updates variables in your show action, provided that you have set the needed associations on the Project model.

    尝试减少控制器中的实例变量数量。例如,您可以在show动作中省略@comments和@updates变量的使用,前提是您已在Project模型上设置了所需的关联。

    has_many :comments
    has_many :updates, class_name: ProjectUpdate
    

    Then in the view you can refer to them as @project.comments and @project.updates without the need to store them separately. It will clean up the controller a bit. Also, if you want to ensure that the comments and updates are not lazy-loaded, you can load them together with the project by calling @project = Project.includes(:comments, :updates).find(params[:id]).

    然后在视图中,您可以将它们称为@ project.comments和@project.updates,而无需单独存储它们。它会稍微清理一下控制器。此外,如果要确保注释和更新不是延迟加载的,可以通过调用@project = Project.includes(:comments,:updates).find(params [:id])将它们与项目一起加载。 。

Hope that would be helpful. I'll update the answer would I have something more concrete on the problem.

希望会有所帮助。我会更新答案,我会在问题上找到更具体的内容。