# Help! cannot display model's attr\_accessor

**URL:** <https://www.ruby-forum.com/t/help-cannot-display-models-attr-accessor/178786>\
**Category:** Rails\
**Created:** [November 11, 2009, 10:17am UTC](https://www.ruby-forum.com/t/help-cannot-display-models-attr-accessor/178786 "2009-11-11T10:17:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 11, 2009, 10:17am UTC](https://www.ruby-forum.com/t/help-cannot-display-models-attr-accessor/178786/1 "2009-11-11T10:17:15Z")

</div>

hi,

i m new to ruby on rails, this question might sound stupid, but i m  
really confused by the relationship between attributes of the model  
and the column in that model table.

i have two tables ‘institutions’ and ‘alerts’ as below:

Institutions:  
id site created\_at updated\_at  
1 default null null

alerts  
id institution\_id name value  
1 1 testname testvalue

# the corresponding models are:

alert.rb  
class Alert \< ActiveRecord::Base  
belongs\_to :institution

attr\_accessor :name, :value

def self.find\_all(institution\_id)  
find(:all, :conditions =\> “institution\_id = ‘#{institution\_id}’”)  
end  
end

=================================================  
institution.rb  
class Institution \< ActiveRecord::Base  
has\_one :alerts

# def find\_alerts(institution\_id) Alert.find\_all(institution\_id) end end

in app/views/institutions/ i have edit.html.erb

# Editing institution

\<% form\_for(@institution) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :site %\>  
 \<%= f.text\_field :site %\>

\<%= f.label :id %\>  
 \<%= f.text\_field :id %\>

\<% for alert in @institution.find\_alerts(@institution.id) %\>

\<%=h alert.name %\>

 \<% end %\> 

\<%= f.submit 'Update' %\>

\<% end %\>

\<%= link\_to ‘Show’, @institution %\> |  
\<%= link\_to ‘Back’, institutions\_path %\>

however it can only show the institution’s info but not alert’s info.

from irb i tried:  
a = Alert.find\_all(1)  
it showed me the correct record  
but if i tried: a.name  
it complained: NoMethodError: undefined method ‘name’, but i already  
defined attr\_accessor :name, :value in alert.rb, how come it still  
complained?

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 11, 2009, 10:21am UTC](https://www.ruby-forum.com/t/help-cannot-display-models-attr-accessor/178786/2 "2009-11-11T10:21:57Z")

</div>

On Nov 11, 12:26 am, mars [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> institution.rb  
> class Institution \< ActiveRecord::Base  
> has\_one :alerts
> 
> def find\_alerts(institution\_id)  
> Alert.find\_all(institution\_id)  
> end  
> end

orthogonal to this, but your associations are borked - that should be

has\_many :alerts

and then you can do away with that find method and do  
@institution.alerts

> from irb i tried:  
> a = Alert.find\_all(1)  
> it showed me the correct record  
> but if i tried: a.name

that dies because your find\_all method returns an array and arrays  
don’t have a name method.  
Your view doesn’t display the name because you’ve put attr\_accessor in  
your model: Active Record creates accessor functions for you, by using  
attr\_accessor you’ve overridden those with versions that don’t look at  
the values Active Record fetches from the database.

Fred
