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 8687229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:04:10+00:00 2026-06-12T23:04:10+00:00

Here is the structure I’m working with: app/models/model.rb class Model < ActiveRecord::Base attr_accessor :some_var

  • 0

Here is the structure I’m working with:

app/models/model.rb

class Model < ActiveRecord::Base
    attr_accessor :some_var
end

app/models/model_controller.rb

class ModelsController < ApplicationController
    def show
        @model = Model.find(params[:id])
        @other_var
        if @model.some_var.nil?
            @model.some_var = "some value"
            @other_var = "some value"
        else
            @other_var = @model.some_var
        end
    end
end

Whenever I run this code (e.g. the show method), the if clause is evaluated to be true (e.g. @model.some_var == nil). How do I get around this? Is there something wrong in my assumption of how attr_accessor works?

  • 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-12T23:04:11+00:00Added an answer on June 12, 2026 at 11:04 pm

    attr_accessor is a built-in Ruby macro which will define a setter and a getter for an instance variable of an object, and doesn’t have anything to do with database columns with ActiveRecord instances. For example:

    class Animal
      attr_accessor :legs
    end
    
    a = Animal.new
    a.legs = 4
    a.legs #=> 4
    

    If you want it to be saved to the database, you need to define a column in a migration. Then ActiveRecord will create the accessor methods automatically, and you can (should) remove your attr_accessor declaration.

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

Sidebar

Related Questions

Here is model structure: Client is User, Client can be corporate or person: class
For example I have a file name call A.java. Here its structure: public class
Here is my project structure: /app --/lib ----/porter.rb --/spec ----/porter_spec.rb In file porter_spec.rb i
Here is the structure of the HTML <div class=submenu> <ul> <li></li> <li></li> </ul> <ul>
Here's the structure I'm working with: directory/ script.py subdir/ __init__.py myclass01.py myclass02.py What I
Here is a structure used in a program: struct basic_block { void * aux;
okey so here is my structure. one.php two.php three.php one.php includes both two.php and
I have a document structure {'text': 'here is text', 'count' : 13, 'somefield': value}
I have a complex form in Ruby on Rails 2.3.5, here's the structure for
Here is the simplified table structure of PRICES : SKU PriceType FromDate ToDate Price

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.