help! cannot display model's attr_accessor

View: New views
2 Messages — Rating Filter:   Alert me  

help! cannot display model's attr_accessor

by mars-13 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

<h1>Editing institution</h1>

<% form_for(@institution) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :site %><br />
    <%= f.text_field :site %>
  </p>

  <p>
    <%= f.label :id %><br />
    <%= f.text_field :id %>
  </p>

  <% for alert in @institution.find_alerts(@institution.id) %>
  <p>
    <%=h alert.name %>
  </p>
  <% end %>

  <p>
    <%= f.submit 'Update' %>
  </p>
<% 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!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: help! cannot display model's attr_accessor

by Frederick Cheung-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Nov 11, 12:26 am, mars <marsg...@...> 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



> it complained: NoMethodError: undefined method 'name', but i already
> defined attr_accessor :name, :value in alert.rb, how come it still
> complained?
>
> Thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---