Fetching model's attributes (simple problem)

I have created the attr_accessors for everything I need but still when
I try to get some of the attributes, it returns nil. I know they have a
value since I can check the database with external program.

For example:

object = Object.find(id)
object.id # works
object.name # works
object.read_by_default # doesnt work

I’ve been having this on and off now and it’s driving me crazy. I must
be doing something very slightly but fundamentally wrong. Any ideas? I’m
a bit self-conscious about showing the actual code but it’s as simple as
my example above.

RoR newbie wrote:

I have created the attr_accessors for everything I need but still when
I try to get some of the attributes, it returns nil. I know they have a
value since I can check the database with external program.

For example:

object = Object.find(id)
object.id # works
object.name # works
object.read_by_default # doesnt work

I’ve been having this on and off now and it’s driving me crazy. I must
be doing something very slightly but fundamentally wrong. Any ideas? I’m
a bit self-conscious about showing the actual code but it’s as simple as
my example above.

You don’t need attr_access for an ActiveRecord::Base object, AR already
gives that to you for free. By adding your own accessors you’re actually
stopping the AR stuff from having.
By the way, not showing your code is a bit silly: it just makes it a lot
harder for the folks trying to help you to understand what’s happening.

Fred

RoR newbie wrote:

I have created the attr_accessors for everything I need but still when
I try to get some of the attributes, it returns nil. I know they have a
value since I can check the database with external program.

For example:

object = Object.find(id)
object.id # works
object.name # works
object.read_by_default # doesnt work

I assume read_by_default is a boolean. Are you sure you’re testing that
this attribute is not nil, and not that the value is true?

Chris

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs