Comparing between different attributes. programming noobie

Hi currently i am have a app that checks the cheapest price between
colums/attributes

if model.attribute1 < model.attribute 2

result = model.attribute 1

else

if model …
result = model. att2

else

if model att3 <

is there any easier method to do this??

the attributes have different names so i don’t know use a loop and
change the attribute via a counter.

On 20 Aug 2008, at 11:41, tyliong wrote:

if model …
change the attribute via a counter.
So your models have a number of columns and you want to find the
smallest one?

Two possible ways: listing the good attribute names

model.attributes.values_at(‘name1’, ‘name2’, 'name3).min

Or all columns except the listed ones

model.attributes.except(‘id’, ‘something’).values.min

or all attributes matching a certain pattern

model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min

Fred

Thanks that was what I needed to know!

Just another question:
How about if I need to call out the name of the column as well as the
value
it shows if that column has the minimum value.

Thanks a lot. I’ll try implementing it in my code soon.

Another question about .attributes

Now that I am working on it the I don’t know to call out the header name
of
the coloumn.

Btw I am looking through the ruby api but I just can’t seem to find this
info, where do u learn .attribute from??? Could u also send me the link?

On 20 Aug 2008, at 11:55, Tan YL wrote:

Thanks that was what I needed to know!

Just another question:
How about if I need to call out the name of the column as well as
the value
it shows if that column has the minimum value.

something like model.attributes.(except or slice or reject).sort {|
first, second| first[1] <=> second[1]}
This will return a sorted array of key value pairs.

Fred

I’m looking through the ruby api one at the moemtn. When I look for
“attributes” they show me the methods but where is the .attribute
version?

On 1 Sep 2008, at 09:53, Tan YL wrote:

link?
if you’re looking through the ruby api you won’t find it because you
need to be looking at the rails ones. There are quite a few places
which display the docs in a variety of formats, including
http://api.rubyonrails.com/

Fred

So where do I learn to use the .attribute from and its syntax ?

On 1 Sep 2008, at 10:45, Tan YL wrote:

So where do I learn to use the .attribute from and its syntax ?

You don’t because there is no such method (unless we are talking at
cross purposes)

Fred

On 1 Sep 2008, at 10:38, Tan YL wrote:

I’m looking through the ruby api one at the moemtn. When I look for
“attributes” they show me the methods but where is the .attribute
version?

.attribute doesn’t exist

Fred

Not the method but the “attribute” in “model.attributes.value.min” (i
don’t
know what u call this type of object)

Let me try to rephrase: where do u learn the how to manipulate the model
with those extensions. For instance (model. “attribute”. “value”) where
do u
learn that u can add those values to the model so that u can bring
something
like the attribute or the cell value of the record out I can’t seem to
find
any resource on it. The api gives methods but not this(or maybe I can’t
seem
to find where it is in the api)

On 1 Sep 2008, at 11:01, Tan YL wrote:

can’t seem
to find where it is in the api)

if you have an instance of a model, some of the methods are part of
the api, but some are added at runtime based on information from the
database. So for example if your users table has a name column then
instances of User will have a name method, but there is nowhere you
can look that up. There are a few other places where rails generates
methods at run time (eg dynamic finders).

Fred

Thanks for your help.

On 1 Sep 2008, at 10:51, Tan YL wrote:

Not the method but the “attribute” in
“model.attributes.value.min” (i don’t
know what u call this type of object)

Sorry, but I’ve no idea what you’re going on about.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs