# How to create "custom" fields in Model?

**URL:** <https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513>\
**Category:** Rails\
**Created:** [July 16, 2007, 8:21am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513 "2007-07-16T08:21:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 16, 2007, 8:21am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/1 "2007-07-16T08:21:03Z")

</div>

How to create “custom” fields in Model? like sum, based on quantity  
and price.  
I guess it should be something like:

```
    def summ
            price * quantity
    end

```

Am I right?

If so, How to use them in :conditions parameter of the  
ActiveRecord::Base::find method? Is it possible it all?

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 16, 2007, 11:22am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/2 "2007-07-16T11:22:14Z")

</div>

if you want to do that, you’re gonna have to learn some SQL.

# open up your SQL browser/command line and try

SELECT (price \* quantity) FROM table\_name;

SELECT table\_name.\*, (price \* quantity) FROM table\_name;

SELECT table\_name.\*, (price \* quantity) AS total\_price FROM table\_name;

# SELECT \* FROM table\_name WHERE (price \* quantity) \> 30.00;

now go into ruby script/console

===========================  
object = Model.find(:first, :select =\> "(price \* quantity) AS  
total\_price)  
object.total\_price

# object = Model.find(:all, :conditions =\> “(price \* quantity) \> 30”)

hopefully that’ll give you a good start

🙂

gneeot wrote:

> How to create “custom” fields in Model? like sum, based on quantity  
> and price.  
> I guess it should be something like:
> 
> ```
> def summ
> price * quantity
> end
> 
> ```
> 
> Am I right?
> 
> If so, How to use them in :conditions parameter of the  
> ActiveRecord::Base::find method? Is it possible it all?

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 16, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/3 "2007-07-16T12:51:53Z")

</div>

Thnak you Matthew, I know SQL a bit :), but what to do if field is  
much more complicated, for ex. I have few more complex fields in my  
Order model:

def number\_of\_produced\_items  
total = 0  
productions.each do |p|  
total += p.quantity if p.start\_time and p.finish\_time  
end  
total  
end

def number\_of\_dispatched\_items  
total = 0  
dispatches.each do |p|  
total += p.quantity  
end  
total  
end

def number\_of\_items\_in\_progress  
total = 0  
productions.each do |p|  
total += p.quantity if not p.finish\_time  
end  
total  
end

def enough\_items\_to\_be\_dispatched?  
if (number\_of\_produced\_items \> 0 and number\_of\_dispatched\_items \<  
number\_of\_produced\_items) then true else false end  
end

,and don’t want to redefine them in every find method, it takes a lot  
of time 🙂

for ex., one of these fields I redefined for one of the combo boxes as  
quite long SQL:

select(“dispatch”, “order\_id”,  
Order.find\_by\_sql("SELECT \* FROM orders o WHERE id in  
(  
SELECT p.order\_id FROM productions p  
WHERE p.start\_time is not NULL and p.finish\_time is not NULL  
GROUP BY p.order\_id  
HAVING sum(p.quantity) \> IFNULL(  
(  
SELECT sum(d.quantity) FROM dispatches d  
WHERE d.order\_id = p.order\_id  
GROUP BY d.order\_id  
) ,0)  
)

Does anyone have any Idea how to simplify it all?

–  
Yuriy

On Jul 16, 12:22 pm, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 16, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/4 "2007-07-16T16:59:31Z")

</div>

Yuriy,

I think in certain cases it’s absolutely kosher to just add methods like  
you  
did. Just make sure it doesn’t create additional SQL select calls for  
you,  
for example, use :include =\> :productions when calling find for your  
model.

Cheers,  
Yuri

On 7/16/07, gneeot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> p.finish\_time  
> end  
> if (number\_of\_produced\_items \> 0 and number\_of\_dispatched\_items  
> select(“dispatch”, “order\_id”,  
> ) ,0)
> 
> > hopefully that’ll give you a good start
> > 
> > > ```
> > > end
> > > 
> > > ```

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 18, 2007, 9:35am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/5 "2007-07-18T09:35:52Z")

</div>

Thank you. I think :include is what I’ve been missing sometimes. Will  
consider rewriting some staff to SQL.

Regards,  
Yuriy

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 16, 2007, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/6 "2007-07-16T17:13:32Z")

</div>

Hi

It depends on the size of the related record set (productions).  
Looping through a huge recordset will probably be less performant  
(and more cpu and memory intensive) than using SQL to get what you  
want. However, don’t look for problems and bottlenecks as long as  
they’re not there. Switching these methods in your model to an SQL  
query instead of what you have now should be a trivial task, so if  
there’s ever a need to increase performance, it will be the time to  
look at other options such as an SQL query. For now, this is probably  
just fine.

On 16 Jul 2007, at 16:59, Yuri L. wrote:

> Thnak you Matthew, I know SQL a bit :), but what to do if field is  
> end  
> total = 0  
> end  
> Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 18, 2007, 11:24am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/7 "2007-07-18T11:24:20Z")

</div>

as I was saying before;

object = Model.find(:first, :select =\> "(price \* quantity) AS  
total\_price)

you could refactor this

def self.find\_with\_total\_price(_args)  
self.with\_scope(:find =\> {  
:select =\> "_," + total\_price\_select  
}) do  
self.find(\*args)  
end  
end

def total\_price\_select  
“(price \* quantity) AS total\_price”  
end

# make sure our reader method outputs an int

# else it’d just return a string

def total\_price  
read\_attribute(:total\_price).to\_i  
end

and do something similar for the rest.

gneeot wrote:

> Looks like :find only constructs SQL. I’m wondering is It possible to  
> rewrite some of fields in my model using SQL, e.g.  
> “number\_of\_items\_in\_progress” and then use them in :find?

---

<div class="post-metadata">

**Author:** ![gneeot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c89c15/32.png) [@gneeot](https://www.ruby-forum.com/u/gneeot)\
**Post date:** [July 18, 2007, 10:07am UTC](https://www.ruby-forum.com/t/how-to-create-custom-fields-in-model/109513/8 "2007-07-18T10:07:57Z")

</div>

Looks like :find only constructs SQL. I’m wondering is It possible to  
rewrite some of fields in my model using SQL, e.g.  
“number\_of\_items\_in\_progress” and then use them in :find?
