【问题标题】:Rails - View not updating with new data until I clear the cacheRails - 在我清除缓存之前查看不更新新数据
【发布时间】:2012-09-21 05:25:37
【问题描述】:

我有一个数据库,其中包含具有 has_and_belongs_to_many 关系的用户和组。添加新组时,它会被创建,但用户对该组的成员资格似乎不会传播,直到我清除缓存或使用隐身窗口登录。我知道它已正确保存,只是在清除缓存之前似乎没有加载。这只是最近才开始发生,我不知道为什么!任何帮助将不胜感激。

来自模型:

class User < ActiveRecord::Base
    has_many :services
    has_many :recipes
    has_and_belongs_to_many :groups
    attr_accessible :recipes, :groups
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :recipes
  attr_accessible :description, :title, :recipe, :picture, :featured, :user_id
end

创建组方法:

def create
    @user = User.find(current_user.id)
    @group = Group.new(params[:group])
    @group.user_id = @user.id   
    @user.groups << @group

    redirect_to group_path(@group)
  end

显示用户的组成员资格——在缓存被清除之前不会更新:

<% @user.groups.each do |group| %>
<% if group %>
    <p class="group-title"><a href="<%=  group_path(group) %>"><%= group.title %></p>
        <% @latestpic = Recipe.where("group_id = ?", group).limit(1).order("created_at DESC") %>
        <% if @latestpic.exists? %>
            <% @latestpic.each do |pic| %>
                <%= image_tag(pic.picture.url(:medium)) %>  
            <% end %></a>
        <% else %>
            <%= image_tag "http://placehold.it/300x300" %>
        <% end %>
        <br></br>

<% end %>
<% end %>

【问题讨论】:

  • @AdamEberlin -- 我是个 Rails 菜鸟。我将如何检查?
  • 您是使用 RAILS 独立服务器还是其他服务来为您的 RAILS 应用程序提供服务?什么操作系统?
  • @AdamEberlin - 我正在使用瘦 Web 服务器(v1.4.1 代号 Chromeo)。我在 Mac OS 上。此问题发生在本地以及托管在 Heroku 上时。

标签: ruby-on-rails caching has-and-belongs-to-many


【解决方案1】:

在您的模型中,您具有“拥有并属于多个”关系,这意味着您的用户可以在 n 个组中,并且您的组包含 n 个用户。

@group.user_id

如果您在“组”表中创建了 user_id 列,则可以删除它,因为一个组包含 n 个用户。您必须像这样在用户和组之间使用表格:

create_table :group_users, :id => false do |t|
  t.references :group, :null => false
  t.references :user, :null => false
end

然后像下面那样重构你的控制器:

def create
  @group = current_user.groups.build(params[:group])

  if @group.save
    redirect_to @group, notice: 'Group was successfully created.'
  else
    render action: "new"
  end
end

这将创建一个包含您当前用户的组。在您的方法中,您忘记保存修改。因为运算符 = 和

你也可以在你的视图中重构很多东西,但这不是问题,我们会保持原样。

现在可以用了吗?

【讨论】:

    【解决方案2】:

    可能这个答案已经过时,但可能对最终来到这里的谷歌用户有用:

    当 Rails(4.2 for me) 更新 Has-And-Belongs-To-Many 关联时,它不会更改根记录的 updated_at 值。示例:

    # This does not change @user.updated_at value 
    @user.update_attributes(group_ids: [1, 2, 3])
    

    每个 ActiveRecord 对象都有一个特殊的cache_key,通常使用updated_at 的值构建,缓存的失效基于此。因此,如果我们仅更改 HABT,它不会使缓存无效。

    此处可能的解决方案 - 如果 HABTM 已更改,请手动调用 @user.touch

    【讨论】:

      【解决方案3】:

      如果有人因为创建或删除后数据不显示而来到这里,那么您需要执行以下操作以在执行这些操作后更新缓存:

        # in your model file
        after_save    :expire_model_all_cache
        after_destroy :expire_model_all_cache
      
        def expire_model_all_cache
          Rails.cache.delete("your cache name")
        end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 2019-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多