Forum: Ruby on Rails Attributes for a model which are excluded from DB?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris Birkinshaw (Guest)
on 2006-06-14 13:05

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
Alex Wayne (Guest)
on 2006-06-14 18:11
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,
>
> Chris

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.

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.
Jeremy Evans (Guest)
on 2006-06-14 18:31
(Received via mailing list)
On 6/14/06, Alex Wayne <rubyonrails@beautifulpixel.com> wrote:
>     def bar
>       @bar
>     end
>     def bar=(value)
>       @bar = value
>     end
>   end

It 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 (Guest)
on 2006-06-14 18:56
(Received via mailing list)
Hi Alex,

a clarification below:


On 14-Jun-06, at 9:11 AM, Alex Wayne wrote:
>     @bar = nil
>
>   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
This topic is locked and can not be replied to.