Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3692048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:13:03+00:00 2026-05-19T04:13:03+00:00

In Rails, blocks can be used as callbacks , e.g.: class User < ActiveRecord::Base

  • 0

In Rails, blocks can be used as callbacks, e.g.:

class User < ActiveRecord::Base
  validates_presence_of :login, :email

  before_create {|user| user.name = user.login.capitalize
    if user.name.blank?}
end

When a block is used like this, is there any use for break and return? I’m asking because normally in a block, break will break out of the loop, and return will return from the enclosing method. But in a callback context, I can’t get my head round what that means.

The Ruby Programming Language suggests that return could cause a LocalJumpError but I haven’t been able to reproduce this in a Rails callback.


Edit: with the following code I’d expect a LocalJumpError, but all the return does is stop the rest of the callback executing.

class User < ActiveRecord::Base
  validates_presence_of :login, :email

  before_create do |user|
    return
    user.name = user.login.capitalize
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:13:03+00:00Added an answer on May 19, 2026 at 4:13 am

    Actually it’s kind of interesting…

    When you use before_create in Rails 3, we take the block or lambda that you give us and convert it into a method. We then invoke the method with the current ActiveRecord object, for backwards compatibility with the old Rails approach.

    As a result, the following is equivalent to your snippet:

    class User < ActiveRecord::Base
      validates_presence_of :login, :email
    
      before_create do
        self.name = login.capitalize if name.blank?
      end
    end
    

    Because of this behavior, you can call return from the block, and it will behave the same as a return in a normal method (because it is a normal method).

    In general, next in a block "returns" from the block.

    The specific behavior of normal blocks is:

    • When you call next, you are skipping the rest of the block, and returning control to the method that invoked the block.
    • When you call break, you are skipping the rest of the block, and also immediately returning from the method that invoked the block.

    You can see that behavior in normal iterators:

    value = [1,2,3,4].each do |i|
      next if i == 2
      puts i
    end
    

    In this case, value will be [1,2,3,4], the normal return value of the each method, and the output will be:

    1
    3
    4
    

    In the case of break:

    value = [1,2,3,4].each do |i|
      break if i == 2
      puts i
    end
    

    In this case, the value will be nil, since the break also immediately returned from the each method. You can force a return with a specific value by using break n, which will make value the same as n. The output in the above case will be:

    1
    

    The important thing is that next and break do not just apply to iterators, although their semantics are designed to behave like their equivalents in C in the case of iterators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rails uses the concept of migrations to deal with model changes using the ActiveRecord
Rails comes with a handy session hash into which we can cram stuff to
Rails gurus: I've just discovered named_scope thanks to another SO user. :) I'd like
I have recently started a project in Ruby on Rails. I used to do
When writing a helper for printing javascript that can be used from both other
In my rails app I have a User model. In that model I have
In some Rails model definitions, there is a comment block at the top that
Rails comes with RESTful resources out of the box, but do you use those
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect
Rails is a great platform, but it just doesn't have the history that Perl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.