I am trying to access images from paperclip using establish_connection.
Here is my code of model article.rb
class Article < ActiveRecord::Base
if Rails.env.production?
establish_connection SECONDARY_DB_CONFIG
else
establish_connection "article_#{Rails.env}"
end
has_many :assets, dependent: :destroy
accepts_nested_attributes_for :assets
validates_associated :assets
end
file asset.rb
class Asset < ActiveRecord::Base
if Rails.env.production?
establish_connection SECONDARY_DB_CONFIG
else
establish_connection "article_#{Rails.env}"
end
belongs_to :article, polymorphic: true
has_attached_file :image, :styles => { :large=> "1200x700",:medium => "800x" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/png", "image/jpeg"]
end
And Code in view
<% Article.all.each do |article| %>
<div class="project-item col-sm-6 col-md-4 col-lg-3">
<% if article.assets.length > 0 %>
<img src="<%= article.assets.last.image.url(:medium) %>" alt="<%=article.name%>" />
<% end %>
<div class="hover-title">
<h2 class="project-title"><%= article.name%></h2>
<p><%= property.short_desc %></p>
</div>
</div>
<% end %>
it throws error like this
undefined method `has_attached_file' for Asset (call 'Asset.connection' to establish a connection):Class
You need to install paperclip gem in the existing project, copy and configure AWS S3 information from the project which you need to access.
In Gemfile
In production.rb