DEV Community

Cover image for Rails 7 - Sneak Peek [part1]
fedeagripa
fedeagripa

Posted on

2 1

Rails 7 - Sneak Peek [part1]

New method

# replaces the common pattern of finding sibling objects without one self:
relation.excluding(post, another_post) 
Enter fullscreen mode Exit fullscreen mode

Lets see how we can use it

A simple example for this shows can you simply filter other posts in your usually built onboarding project and it seems quite cool

class Post
  def simliar_posts
    user.posts.excluding(self)
  end
end
Enter fullscreen mode Exit fullscreen mode

Lets actually think if it useful in a common place

But wait a second... we have a really messy implementation of BROADCAST in ActionCable where we need to filter our own sent messages everytime, and this new friend may look promising to use.
What if we implement a MULTICAST, or even add a parameter to our usual broadcast_to method in ActionCable making it look like this source:

def broadcast(message, exclude_self: true)
  server.logger.debug { "[ActionCable] Broadcasting to #{broadcasting}: #{message.inspect.truncate(300)}" }

  payload = { broadcasting: broadcasting, message: message, coder: coder }
            ActiveSupport::Notifications.instrument("broadcast.action_cable", payload) do
  encoded = coder ? coder.encode(message) : message
    if exclude_self
      server.pubsub.exclude(self).broadcast broadcasting, encoded
    else
      server.pubsub.broadcast broadcasting, encoded
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

THOUGHTS??

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay