Rails: pagy ignores attr_accessor

155 Views Asked by At

I need to show attr_accessor value on index page. for pagination I'm using pagy gem. Below is the code block:

def index
    @posts = Post.all
    @posts.map do |post|
      post_info = get_post_info(post.url)
      post.update_attribute(:icon, post_info["icon"])
      post.update_attribute(:subtitle, post_info["subtitle"])
    end
    print @posts[0].icon
    @pagy, @posts = pagy(@posts)    
  end

printing icon of first post gives the correct url in controller.

When I use post.icon in view file, its nil. If I send @posts directly without pagy, it works as expected. How to solve this?

Here is the code without pagy

def index
    @posts = Post.all
    @posts.map do |post|
      post_info = get_post_info(post.url)
      post.update_attribute(:icon, post_info["icon"])
      post.update_attribute(:subtitle, post_info["subtitle"])
    end
  end

Here is my view code:

index.html.erb

<div class="">
      <%= render partial: "posts/index", collection: @posts, as: :post %>
  </div> 

_index.html.erb as below

  <div class="flex mb-4">
    <div class="rounded float-left mr-3 my-3" style=";">
      <% if !post.icon.nil? %>
        <%= image_tag post.icon, class: "app-icon float-right w-full h-full rounded-3xl", style: "width: 150px;" %>
      <% else %>
         <%= image_tag 'post.svg', class: "app-icon w-full h-full", style: "width: 150px;" %>
      <% end %>
    </div>
    <div style="width: 85%" class="mt-5 pt-3 lg:ml-4">
      <h3 class="lg:text-xl text-sm font-semibold text-gray-50"><%= post.title %></h3>
      <div class="lg:text-base text-xs text-gray-50"> <%= post.subtitle %> </div>
    </div>
  </div>
0

There are 0 best solutions below