I have Post model:
class Post < ApplicationRecord
belongs_to :user
has_rich_text :content
end
On Post#index I want to display a post with title, an image and description. For image and description, how I could get those from ActionText?
I think get first image ~ get first active storage blob (belongs to the Post) which content_type is image.
irb(main):017:0> Post.last.content
Post Load (22.6ms) SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` DESC LIMIT 1
ActionText::RichText Load (3.2ms) SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 12 AND `action_text_rich_texts`.`record_type` = 'Post' AND `action_text_rich_texts`.`name` = 'content' LIMIT 1
Traceback (most recent call last):
1: from (irb):17
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 4.0ms | Allocations: 54)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.6ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.8ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.3ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/content/_layout.html.erb (Duration: 445.6ms | Allocations: 54464)
NoMethodError (undefined method `first' for #<ActionText::RichText:0x00007fb0a9e33620>)
irb(main):018:0> Post.last.content.body
Post Load (1.6ms) SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` DESC LIMIT 1
ActionText::RichText Load (3.4ms) SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 12 AND `action_text_rich_texts`.`record_type` = 'Post' AND `action_text_rich_texts`.`name` = 'content' LIMIT 1
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 54)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.3ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.7ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/content/_layout.html.erb (Duration: 115.7ms | Allocations: 53442)
=> #<ActionText::Content "<div class=\"trix-conte...">
Ref: for description I see this question
Thanks to Roland for the answer, it should be accepted.
To avoid N+1 queries add with_rich_text_[ATTRIBUTE]_and_embeds. https://edgeguides.rubyonrails.org/action_text_overview.html#avoid-n-1-queries
Content in my model is in the text attribute.
posts_controller.rb
@post = Post.with_rich_text_text_and_embeds.find(params[:id])
show.html.erb
<% if !post.text.embeds.empty? %>
<%= image_tag post.text.embeds.find{|embeds| embeds.image?}.variant(resize_to_limit: [200, 200]) %>
<% end %>
Will appreciate any more effective solution.
The ActionText model provides the association embeds, so you can just call find on that with the handy method image?
Post.last
.with_rich_text_text_and_embeds # to avoid N+1 queries
.content
.embeds
.find{|embeds| embeds.image?} # to make sure embed is an image
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With