somple question here but pretty confusing on my side. i have a model towns that is mapped to my towns table. can i add an extra variable to the class and access it in my view? basicall what i am trying to do is return a list of towns, and next to each town, display a total number of what i have in each town. ive tried both attr_reader and @total but no avail. here is my view <% for county in @counties %> <h1><%= county.name %></h1> <% for town in county.towns %> <p><%= town.name %></p> <p><%= town.total %></p> <% end %> my town model: class Town < ActiveRecord::Base belongs_to :county attr_reader :total @total def self.get_list @total = 100 find(:all) end end -- Posted via http://www.ruby-forum.com/.
koloa
2006-Aug-12 18:55 UTC
[Rails] Re: adding extra variable to a class, how to access it?
never mind, i figured it out. i realized you can access instance variable directly, you need a ''get'' method. -- Posted via http://www.ruby-forum.com/.
Ross Riley
2006-Aug-14 19:28 UTC
[Rails] Re: adding extra variable to a class, how to access it?
or attr_accessor :variable Ross On 8/12/06, koloa <none@none.com> wrote:> never mind, i figured it out. i realized you can access instance > variable directly, you need a ''get'' method. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ross Riley riley.ross@gmail.com
Jan Eden
2007-Jul-16 22:43 UTC
Re: Re: adding extra variable to a class, how to access it?
Ross Riley wrote:> or attr_accessor :variable > > RossStrangely enough, this does not work for me. I tried: class Page < ActiveRecord::Base belongs_to :author attr_accessor :typus end and was not able to access somepage.typus in my view. However, this worked: class Page < ActiveRecord::Base belongs_to :author def typus=(typus_value) write_attribute(:typus, typus_value) end def typus read_attribute(:typus) end end Why is it that attr_accessor does not work for an AR-based class? Thanks, Jan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jan Eden
2007-Jul-16 22:45 UTC
Re: Re: adding extra variable to a class, how to access it?
In addition to my recent message: I can use somepage.typus (reading/writing) in the controller, but not read it in the view when using attr_accessor. Jan Eden wrote:> Ross Riley wrote: >> or attr_accessor :variable >> >> Ross > > Strangely enough, this does not work for me. I tried: > > class Page < ActiveRecord::Base > belongs_to :author > attr_accessor :typus > end > > and was not able to access somepage.typus in my view. However, this > worked: > > class Page < ActiveRecord::Base > belongs_to :author > def typus=(typus_value) > write_attribute(:typus, typus_value) > end > > def typus > read_attribute(:typus) > end > end > > Why is it that attr_accessor does not work for an AR-based class? > > Thanks, > Jan-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann
2007-Jul-16 23:05 UTC
Re: adding extra variable to a class, how to access it?
Jan Eden schrieb:> Ross Riley wrote: >> or attr_accessor :variable >> >> Ross > > Strangely enough, this does not work for me. I tried: > > class Page < ActiveRecord::Base > belongs_to :author > attr_accessor :typus > end > > and was not able to access somepage.typus in my view. However, this > worked: > > class Page < ActiveRecord::Base > belongs_to :author > def typus=(typus_value) > write_attribute(:typus, typus_value) > end > > def typus > read_attribute(:typus) > end > end > > Why is it that attr_accessor does not work for an AR-based class? > > Thanks, > Jan >Hi Jan, did you try to access a persistent or a non-persistent attribute? What do you get when you @page.typus in your view, I guess nil unless you''ve done something like @page.typus = ''something'' before... Regards Florian --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann wrote:> Jan Eden schrieb: >> end >> def typus >> read_attribute(:typus) >> end >> end >> >> Why is it that attr_accessor does not work for an AR-based class? >> >> Thanks, >> Jan >> > > Hi Jan, > > did you try to access a persistent or a non-persistent attribute? > > What do you get when you @page.typus in your view, I guess nil unless > you''ve done something like @page.typus = ''something'' before... > > Regards > FlorianHi Florian, it''s a non-persistent attribute, calculated on the fly based on another attribute (it''s actually a string to be used as a CSS classname). @page.typus won''t work, because I use the instance variable @children to store a list of Page objects (which in turn have the typus attribute): This is what happens in the controller: @children = Page.find(:all, :conditions => "mother_id = #{page.id}", :order => "position asc, title asc") @children.each do |child| child.author = nil if child.author == page.author child.typus = (child.visible_children != nil) ? ''Node'' : ''Page'' end This does set @typus for each child page correctly (I inspected the children), but I cannot access the instance variable (like child.typus) when doing only "attr_accessor :typus" in the model. Thanks, Jan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann
2007-Jul-16 23:52 UTC
Re: adding extra variable to a class, how to access it?
Hi Jan> @children.each do |child| > child.author = nil if child.author == page.author > child.typus = (child.visible_children != nil) ? ''Node'' : ''Page'' > endIn this case I''d drop child.typus = (child.visible_children != nil) ? ''Node'' : ''Page'' and def typus in the model itself: def typus visible_children ? ''Node'' : ''Page'' end What''s your code in the view? I can''t understand why accessor shouldn''t work... Regards Florian --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann wrote:> Hi Jan > >> @children.each do |child| >> child.author = nil if child.author == page.author >> child.typus = (child.visible_children != nil) ? ''Node'' : ''Page'' >> end > > In this case I''d drop > > child.typus = (child.visible_children != nil) ? ''Node'' : ''Page'' > > and def typus in the model itself: > > def typus > visible_children ? ''Node'' : ''Page'' > end > > What''s your code in the view? I can''t understand why accessor shouldn''t > work...Thanks for the suggestion, that''s definitely an improvement (and it works fine). The view simply has: <% @children.each do |child| %> <li><a class="<%= child.typus %>" href="/pages/<%= child.id %>.html" target="_self"><%= child.title %></a></li> </end> To summarize: - your suggestion (a simple method for each Page instance) works - The AR-specific getter/setter methods using write_attribute/read_attribute work - The generic attr_accessor method to create an attribute does not work (Error: undefined method) Thanks again, Jan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann
2007-Jul-17 08:01 UTC
Re: adding extra variable to a class, how to access it?
I''m very curious about this one. For me, declaring accessors, has always worked in my whole rails code... The objects in children in your view are really instances of Page? Did you make a child.inspect in vour view or child.methods.sort or maybe even child.instance_variable_get(:@typus)? I use inspect and methods(+grep) very often at frontend debugging... Regards Florian --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Florian Aßmann wrote:> I''m very curious about this one. For me, declaring accessors, has always > worked in my whole rails code... > > The objects in children in your view are really instances of Page? Did > you make a child.inspect in vour view or child.methods.sort or maybe > even child.instance_variable_get(:@typus)? > > I use inspect and methods(+grep) very often at frontend debugging... > > Regards > FlorianOk, here''s a simplified version of my setup (the model lists the three variants of implementing an attribute, of which variant 1) does not work): ## Model: class Page < ActiveRecord::Base belongs_to :author # 1) # attr_accessor :typus # 2) # def typus=(typus_value) # write_attribute(:typus, typus_value) # end # # def typus # read_attribute(:typus) # end # 3) # def typus # visible_children ? ''Knoten'' : ''Seite'' # end end The controller needs to iterate over each child for variants 2) and 3) ## Controller: class PagesController < ApplicationController def index page = Page.find(:first, :conditions => "id = #{params[''id'']}") @title = page.title @content = page.content @children = Page.find(:all, :conditions => "mother_id = #{page.id}", :order => "position asc, title asc") # @children.each { |child| child.typus = child.visible_children ? ''Knoten'' : ''Seite'' } end end The view simply uses child.typus for each element of @children: ## View: <h1><%= @title %></h1> <%= @content %> <% @children.each do |child| %> <li><a class="<%= child.typus %>" href="/pages/<%= child.id %>.html" target="_self"><%= child.title %></a></li> <% end %> Cheers, Jan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- Delaying initialization of associations until first access
- Overriding AR read/write_attribute - Overridden write_attribute Is Never Called
- Form validation - keepin correct fields displayed on refresh
- ActiveRecord override
- Issue in ActiveRecord generated reader methods