Chris Birkinshaw
2006-Jun-14 11:06 UTC
[Rails] Attributes for a model which are excluded from DB?
If I want to do this but not save to the database, how can I go about it? things.each do |thing| changed = someotherqueryresult thing.update_attribute :testattrib, changed end Essentially I want to create a collection called things with some extra attributes that are not saved into the database, as they are just used for sorting in a view and should not be saved (in fact saving them wastes time each time I refresh the view). Can anyone help? Thanks, Chris -- Posted via http://www.ruby-forum.com/.
Alex Wayne
2006-Jun-14 16:11 UTC
[Rails] Re: Attributes for a model which are excluded from DB?
Chris Birkinshaw wrote:> > > If I want to do this but not save to the database, how can I go about > it? > > things.each do |thing| > changed = someotherqueryresult > thing.update_attribute :testattrib, changed > end > > > > Essentially I want to create a collection called things with some extra > attributes that are not saved into the database, as they are just used > for sorting in a view and should not be saved (in fact saving them > wastes time each time I refresh the view). > > Can anyone help? > > Thanks, > > Chrisput this at the top of your model class Foo < ActiveRecord::Base attr_accessor :bar end which is the same as: class Foo < ActiveRecord::Base @bar = nil def bar @bar end def bar=(value) @bar = value end end This creates getters and setters for an instance variable that doesn''t know about the database. foo = Foo.new foo.bar = ''baz'' foo.bar #=> ''baz'' Although this will not work with the update_attrubute methods because those are defined by active record and work only on database fields. Also note that since the value is not stored inthe database, it will be cleared after every request unless it''s in somewhere like the session, which stays around between requests. -- Posted via http://www.ruby-forum.com/.
Jeremy Evans
2006-Jun-14 16:30 UTC
[Rails] Re: Attributes for a model which are excluded from DB?
On 6/14/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:> put this at the top of your model > > class Foo < ActiveRecord::Base > attr_accessor :bar > end > > which is the same as: > > class Foo < ActiveRecord::Base > @bar = nil > def bar > @bar > end > def bar=(value) > @bar = value > end > endIt doesn''t make any difference, but it appears as though you are confusing two types of instance variables (since the three @bar variables do not reference the same variable). The @bar variables inside the functions are local to objects of the class, the @bar variable outside the function is local to the class itself. For example: class Foo < ActiveRecord::Base @bar = 42 # class instance variable def self.bar # class method @bar # class instance variable end def bar # instance method @bar # instance variable end def bar=(value) # instance method @bar = value # instance variable end end irb(main):013:0> Foo.new.bar => nil irb(main):014:0> Foo.bar => 42 Again, this doesn''t affect any points you are trying to make, but it is important to understand how ruby works so you don''t run into problems later.
Trevor Squires
2006-Jun-14 16:56 UTC
[Rails] Re: Attributes for a model which are excluded from DB?
Hi Alex, a clarification below: On 14-Jun-06, at 9:11 AM, Alex Wayne wrote:> > put this at the top of your model > > class Foo < ActiveRecord::Base > attr_accessor :bar > end > > which is the same as: > > class Foo < ActiveRecord::Base > @bar = nil > def bar > @bar > end > def bar=(value) > @bar = value > end > end > > This creates getters and setters for an instance variable that doesn''t > know about the database. > > foo = Foo.new > foo.bar = ''baz'' > foo.bar #=> ''baz'' > > Although this will not work with the update_attrubute methods because > those are defined by active record and work only on database fields. >I think what you''re saying in the statement above is that update_attributes(:bar => ''hello'') will not work. That''s not the case. Under the hood you''ll find that new({}), attributes = {}, update_attribute(x,y) and update_attributes({}) *all* delegate the assignment to your methods. So, in your example, update_attributes(:bar => ''hello'') will call bar=''hello''. Regards, Trevor -- Trevor Squires http://somethinglearned.com> Also note that since the value is not stored inthe database, it > will be > cleared after every request unless it''s in somewhere like the session, > which stays around between requests. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/abf4226f/attachment.html