# ActiveRecord does not load attributes

**URL:** <https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976>\
**Category:** Rails\
**Created:** [December 11, 2005, 1:01pm UTC](https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976 "2005-12-11T13:01:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrono325](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@chrono325](https://www.ruby-forum.com/u/chrono325)\
**Post date:** [December 11, 2005, 1:01pm UTC](https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976/1 "2005-12-11T13:01:03Z")

</div>

Hello everybody!

So this is likely a complete n00bish question as I only started riding  
the rails last Wednesday, but I have been tearing my hair out over  
this one for a number of hours now.

I created a model/table Picture/pictures and followed the guide at  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/57) for uploading the file.  
I have a field “path” which is to be the location relative to “public”  
of the picture. The idea is that the filename is taken from what is  
originally uploaded (in my case, ‘dog.png’). The database is  
successfully updated with the path, but whenever I try:

script/runner ‘puts Picture.find\_by\_id(34).path’

I just get nil. I know that it works otherwise, since switching out  
path for name (another one of the saved fields) works just fine.

Here are some snippets of my code:

# picture.rb

def file=(incoming\_file)  
@temp\_file = incoming\_file  
@filename = sanitize\_filename incoming\_file.original\_filename  
@content\_type = incoming\_file.content\_type  
@path = ‘/pictures/’ + @filename  
end

before\_save :set\_path

attr\_reader :filename  
attr\_accessor :path

def after\_save  
if @temp\_file  
File.open(‘public/’ + @path, “wb”) { |f|  
f.write(@temp\_file.read) }  
end  
end

def before\_destroy  
# delete the file from the filesystem  
File.delete [‘public’,self.path]  
end

private

def sanitize\_filename(file\_name)  
# get only the filename, not the whole path (from IE)  
just\_filename = File.basename(file\_name)  
# replace all non-alphanumeric, underscore or periods with  
underscores  
just\_filename.gsub(/[^\w.-]/,‘\_’)  
end

def set\_path  
self.path = ‘pictures/dog.png’  
end

# END

Thanks in advance for any help and thank you for such an amazing  
framework!

Daniel H.

---

<div class="post-metadata">

**Author:** ![chrono325](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@chrono325](https://www.ruby-forum.com/u/chrono325)\
**Post date:** [December 11, 2005, 1:50pm UTC](https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976/2 "2005-12-11T13:50:08Z")

</div>

Well, you didn’t tell whether your database table contains field ‘path’.

If it does, then attr\_accessor shoudn’t be there and you should not  
assign value to instance variable. You are clobbering Rails auto-  
magic with this.

If it doesn’t, how do you think Rails could get it when model object  
is instantiated? You need to store all model info in database fields,  
or at least enough to restore complete attributes manually in  
after\_initialize.

In short, you should treat ‘path’ either as table field or not, and  
not mix those two approaches.

izidor

---

<div class="post-metadata">

**Author:** ![chrono325](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@chrono325](https://www.ruby-forum.com/u/chrono325)\
**Post date:** [December 11, 2005, 2:06pm UTC](https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976/3 "2005-12-11T14:06:22Z")

</div>

Daniel H. wrote:

> # picture.rb
> 
> attr\_accessor :path  
> …

AR getter and setter methods are auto-created from the DB columns.  
They are not just normal instance variable-backed attributes.  
Your attr\_ definitions will override the AR methods for accessing  
these attributes.

Remove the attr\_ definitions, and in the “file=” method replace  
the "@"s with “self.” for each AR attribute being set.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![chrono325](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@chrono325](https://www.ruby-forum.com/u/chrono325)\
**Post date:** [December 11, 2005, 3:25pm UTC](https://www.ruby-forum.com/t/activerecord-does-not-load-attributes/49976/4 "2005-12-11T15:25:41Z")

</div>

wow, thanks for the quick replies! As you can see, I am still very new  
at this whole Ruby thing.

Deleting the attr\_reader and accessors made everything work quite  
nicely. I am still trying to sort out the difference between variable,  
:variable, @variable, and self.variable in my head.

Thanks once again,  
Daniel H.
