# OOP road block

**URL:** <https://www.ruby-forum.com/t/oop-road-block/164423>\
**Category:** Rails\
**Created:** [April 1, 2009, 10:21am UTC](https://www.ruby-forum.com/t/oop-road-block/164423 "2009-04-01T10:21:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 1, 2009, 10:21am UTC](https://www.ruby-forum.com/t/oop-road-block/164423/1 "2009-04-01T10:21:24Z")

</div>

Hi,

I recently performed a major rewrite of my app to provide better  
encapsulation and more descriptive behavior for my models.

But I also encountered very often a dilemma: does the logic I am writing  
belong to the class, or does it belong to an instance?

I am running an ecommerce website, so I have the following models:

User(id, name, …)  
Order(id, user\_id, status, amount)  
Item(id, order\_id, product\_id, quantity)  
Product(id, price, title, …)

Now I’d like to know if a given User has purchased a given Product.

So I fetch my user and product objects  
user = User.find\_by\_name(‘Fernando’)  
product = Product.find\_by\_title(‘The book of Awesome’)

Then, here is my problem. How would you search if the book was purchased  
by the user? There are few ways of doing it:

- User#find\_if\_user\_purchased\_product?(product\_id)  
And then the search makes a join with Item and Order, but it introduces  
the state of the Order in the condition, it could be ‘paid’, ‘unpaid’.  
And here is my problem: I recently migrated from an integer state (i.e:  
paid = 0, unpaid = 5) to strings. And that logic is in the User model,  
but a User shouldn’t be aware about how Order handles its payment  
status.

Therefore this might mean that I should create a class method:  
Order.find\_paid\_order\_for\_product\_by\_user(product\_id, user\_id)

This only has to know about the product\_id and the user\_id. Now this  
means that my User#find\_if\_user\_purchased\_product(product\_id) code  
becomes something like:

def find\_if\_user\_purchased\_product?(product\_id)  
!!Order.find\_paid\_order\_for\_product\_by\_user(product\_id, self.id)  
end

Doesn’t it seem stupid that 1 method under hood, simply calls another  
method? Don’t I have a problem of how I am searching if my user has  
purchased this product?

How would you handle such problem? It’s not only related to my ecommerce  
shop, it could be anything where a DB search is made across 3 tables of  
relationship.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 1, 2009, 11:05am UTC](https://www.ruby-forum.com/t/oop-road-block/164423/2 "2009-04-01T11:05:29Z")

</div>

I think the problem lies in the ability for the DB to perform a join  
across multiple tables which in fact breaks encapsulation!

Maybe I should break down my DB query that uses joins into smaller  
queries. The code is obviously less efficient, but it’s more  
maintainable, as each model is only concerned with its own stuff, and  
therefore I don’t have to make edits across various models just because  
I changed the name or type of attributes in a single model.

Using joins across multiple models doesn’t feel right anymore.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 1, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/oop-road-block/164423/3 "2009-04-01T14:29:45Z")

</div>

I think this is a common ‘dilemma’ that comes across every now and  
then. “Which model should I ask for this information”? I also think  
there’s no right answer. But here’s my two cents:

In your case, you want to know which a product was purchased by a  
user, so the question could go both ways: Did a user purchase this  
product, or was this product purchased by the user. I think it depends  
on your views and controllers, in other words, what is the main object  
you find out the relationship about? If you’re on a user view and want  
to list purchased products, you might just ask @user.orders.each { |o|  
o.products }.flatten (or some such thing). Similarly,  
@product.orders.each { |o| o.user }.flatten would give you all users  
who purchased that product. (this is all making some assumptions about  
your relationships).

In either case, you are working with either an instance of User, or an  
instance of Product, so I think that that this should not be a class  
method, but an instance method.

Same goes when you’re already dealing with a user instance and a  
product instance and you want to find out if that relationship exists,  
which is more to the point of your original post. How about an  
instance method on the User class called has\_purchased?:

@user.has\_purchased?(@product) =\> true #or false

Here’s a stab at that method…  
#class User \< AR:Base  
def has\_purchased?(product)  
!self.orders.products.find\_by\_id(product).nil?  
end

About joins, I think they are absolutely needed. Some models are more  
“self contained” than others, but since you’re working with a  
relational model, it is natural that some models depend on others, and  
using :joins or :include in your finders makes life easier in many  
cases…

On Apr 1, 5:05 am, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 1, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/oop-road-block/164423/4 "2009-04-01T14:35:37Z")

</div>

It also depends how optimal your SQL ends up.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
