# Getting the business logic right!

**URL:** <https://www.ruby-forum.com/t/getting-the-business-logic-right/85128>\
**Category:** Rails\
**Created:** [February 10, 2007, 7:42am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128 "2007-02-10T07:42:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 10, 2007, 7:42am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/1 "2007-02-10T07:42:08Z")

</div>

Hello,

I am working on a voting system. I wish to have a system that allows  
users to vote every X amount of time. I am using a vote model that  
creates a new record every time a vote is cast. How would i add a check  
to stop users form voting more than once in the X amount of time.

here is the vote model

class CreateVotes \< ActiveRecord::Migration  
def self.up  
create\_table :votes do |t|  
t.column :time, :datetime  
t.column :user\_id, :int  
t.column :celebrity\_id, :int  
end  
end

def self.down  
drop\_table :votes  
end  
end

thanks for your time reading this post!!!

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 10, 2007, 8:06am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/2 "2007-02-10T08:06:22Z")

</div>

Stewart wrote:

> How would i add a check  
> to stop users form voting more than once in the X amount of time.
> 
> here is the vote model
> 
> class CreateVotes \< ActiveRecord::Migration  
> def self.up  
> create\_table :votes do |t|  
> t.column :time, :datetime  
> t.column :user\_id, :int  
> t.column :celebrity\_id, :int  
> end  
> end
> 
> def self.down  
> drop\_table :votes  
> end  
> end

You can do this with validations. Write a custom validation that checks  
the database to see if any other votes conflict with it.

class Vote \< ActiveRecord::Base  
def validate  
if Vote.find(:first,  
:conditions =\> [‘time \< ? AND user\_id = ?’, 1.day.ago,  
user\_id]  
errors.add\_to\_base ‘You cannot vote more than once per day!’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 11, 2007, 10:01am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/3 "2007-02-11T10:01:36Z")

</div>

Hi Alex,

Thanks mate thats exactly what i was looking for.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 12, 2007, 8:04am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/4 "2007-02-12T08:04:22Z")

</div>

Stewart wrote:

> ok one more question.
> 
> My site uses sessions to track all of the user information. This session  
> is saved in a value called current\_user. When a vote is cast i want to  
> record the user that cast the vote so i can check and make sure that  
> user is not voting more than once in a day. I also wish to support guest  
> voting. This would record the ip address of the voting user. Should i  
> set this logic up in the model?
> 
> thanks again for your time

sorry to bump this but this is a very busy forum

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 12, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/5 "2007-02-12T15:16:09Z")

</div>

Stewart wrote:

> Hi Alex,
> 
> Thanks mate thats exactly what i was looking for.

Also, if you call the column created\_at, Rails will automatically fill  
in the time for you.

Alan

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 11, 2007, 10:19am UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/6 "2007-02-11T10:19:28Z")

</div>

ok one more question.

My site uses sessions to track all of the user information. This session  
is saved in a value called current\_user. When a vote is cast i want to  
record the user that cast the vote so i can check and make sure that  
user is not voting more than once in a day. I also wish to support guest  
voting. This would record the ip address of the voting user. Should i  
set this logic up in the model?

thanks again for your time

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [February 12, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/getting-the-business-logic-right/85128/7 "2007-02-12T18:45:17Z")

</div>

Stewart wrote:

> ok one more question.
> 
> My site uses sessions to track all of the user information. This session  
> is saved in a value called current\_user. When a vote is cast i want to  
> record the user that cast the vote so i can check and make sure that  
> user is not voting more than once in a day. I also wish to support guest  
> voting. This would record the ip address of the voting user. Should i  
> set this logic up in the model?
> 
> thanks again for your time

Business logic always goes in the model.
