In my model I''d like to write a method that accesses the attributes by name. A very simple implementation would be class MyModel < ActiveRecord::Base def get_value(attribute) return value_of_attribute end end Then I can call it with m.get_value(:name) to get the value of the name column in the db. I''m not sure what to put in for ''value_of_attribute'' though. How do I do that? Pat
Just use the [] accessor: model = MyModel.find(1) value = model[:attribute_name] Cheers! -DF On 3/19/06, Pat Maddox <pergesu@gmail.com> wrote:> In my model I''d like to write a method that accesses the attributes by > name. A very simple implementation would be > > class MyModel < ActiveRecord::Base > def get_value(attribute) > return value_of_attribute > end > end > > Then I can call it with m.get_value(:name) to get the value of the > name column in the db. I''m not sure what to put in for > ''value_of_attribute'' though. How do I do that? > > Pat > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
What if I''m in the model? I don''t want to just get the value of course..I want a method that will take the name of the attribute, process it a bit, and give the output. Pat On 3/18/06, David Felstead <david.felstead@gmail.com> wrote:> Just use the [] accessor: > > model = MyModel.find(1) > value = model[:attribute_name] > > Cheers! > > -DF > > On 3/19/06, Pat Maddox <pergesu@gmail.com> wrote: > > In my model I''d like to write a method that accesses the attributes by > > name. A very simple implementation would be > > > > class MyModel < ActiveRecord::Base > > def get_value(attribute) > > return value_of_attribute > > end > > end > > > > Then I can call it with m.get_value(:name) to get the value of the > > name column in the db. I''m not sure what to put in for > > ''value_of_attribute'' though. How do I do that? > > > > Pat > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hey Pat, if you just want to get the value out of the AR attributes hash you use self[] (in your model) def upcase_something(sym) self[sym].upcase end Regards, Trevor -- Trevor Squires http://somethinglearned.com On 18-Mar-06, at 4:24 PM, Pat Maddox wrote:> What if I''m in the model? > > I don''t want to just get the value of course..I want a method that > will take the name of the attribute, process it a bit, and give the > output. > > Pat > > On 3/18/06, David Felstead <david.felstead@gmail.com> wrote: >> Just use the [] accessor: >> >> model = MyModel.find(1) >> value = model[:attribute_name] >> >> Cheers! >> >> -DF >> >> On 3/19/06, Pat Maddox <pergesu@gmail.com> wrote: >>> In my model I''d like to write a method that accesses the >>> attributes by >>> name. A very simple implementation would be >>> >>> class MyModel < ActiveRecord::Base >>> def get_value(attribute) >>> return value_of_attribute >>> end >>> end >>> >>> Then I can call it with m.get_value(:name) to get the value of the >>> name column in the db. I''m not sure what to put in for >>> ''value_of_attribute'' though. How do I do that? >>> >>> Pat >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails