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

  • SEARCH
  • Home
  • 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 7952617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:54:51+00:00 2026-06-04T02:54:51+00:00

I was learning some Rspec stuff and accidentally introduced some code into my model

  • 0

I was learning some Rspec stuff and accidentally introduced some code into my model class which I expected to create an error. But to my surprise there were non.

class Address < ActiveRecord::Base
  attr_accessible :city, :country, :person_id, :street, :zip
  validates_presence_of :city, :zip, :street
  before_save :setDefaultCountry

  # -- Beginning strange code --

  if true
     puts "Hey! I shouldn't be able to do this"
  end       

  # -- End of strange code --

  private   
  def setDefaultCountry
    if self.country.blank?
      self.country = "US"
    end
  end
end

This is the rails console output:

Arman$ rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > a = Address.new
Hey! I shouldn't be able to do this
 => #<Address id: nil, street: nil, city: nil, zip: nil, country: nil, person_id: nil, created_at: nil, updated_at: nil> 
1.9.3p194 :002 > 

Why isn’t ruby complaining about strange code being added inside the class definition?

  • 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-06-04T02:54:53+00:00Added an answer on June 4, 2026 at 2:54 am

    That’s just how ruby works. You don’t think that attr_accessible should cause error, do you? But this is just a regular method call! Here’s its source

    # File activerecord/lib/active_record/base.rb, line 1083
    def attr_accessible(*attributes)
      write_inheritable_attribute(:attr_accessible, 
                                  Set.new(attributes.map(&:to_s)) + (accessible_attributes || []))
    end
    

    You can run arbitrary ruby code in the class definition. This is a feature, not a bug 🙂

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

Sidebar

Related Questions

I'm learning and testing some graph libraries and am running into a weird problem(but
I'm learning some DirectX programming by re-implementing some DirectX code into different projects, I
I am learning some COM code and the following code puzzled me. STDMETHODIMP _(ULONG)
I am learning to solve some runtime error with gdb. Here are my questions:
I am just learning some pointers stuff in C and I happened to learn
Some background first. I am currently learning some stuff about monadic parser combinators. While
Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came
So I'm just learning some new stuff in C# & Python. Turns out both
I'm learning some Fluent NHibernate and I've run across the semi-awesome PersistenceSpecification class. I've
I was learning some mongodb stuff, and it's pretty awesome! I decided to try

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.