Getters are being triggered at object initialization. #57

Closed
kushagr opened this Issue Jan 15, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@kushagr

kushagr commented Jan 15, 2014

Hi, I've seem to have run into a problem.
My getters are firing up at object initialization.
For example User.new(name: "Robert")
is actually calling the getter for the name?

def name
 if super.blank?
   self.name = fetch_name_from_some_api
   self.save
 end
return super
end

Is this the intended behaviour.
Cause my code throws an error error cause there isn't an access_token from the api at initialization .

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 15, 2014

Owner

This is the intended behavior. The reason is that NoBrainer performs dirty tracking, so it calls the getter to remember the name_was value.

Perhaps it's better to use an after_initialize callback, or provide a :default on the name field.
Maybe you want to fetch_name_from_some_api only unless new_record?, or unless access_token.

Also, it's a bad idea to call save in here, it may lead to some weird behavior in your app that you don't expect.

Owner

nviennot commented Jan 15, 2014

This is the intended behavior. The reason is that NoBrainer performs dirty tracking, so it calls the getter to remember the name_was value.

Perhaps it's better to use an after_initialize callback, or provide a :default on the name field.
Maybe you want to fetch_name_from_some_api only unless new_record?, or unless access_token.

Also, it's a bad idea to call save in here, it may lead to some weird behavior in your app that you don't expect.

@kushagr

This comment has been minimized.

Show comment
Hide comment
@kushagr

kushagr Jan 15, 2014

Thanks a lot for the suggestion !

kushagr commented Jan 15, 2014

Thanks a lot for the suggestion !

@kushagr kushagr closed this Jan 15, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment