Forum: Ruby on Rails getting the business logic right!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Stewart (Guest)
on 2007-02-10 08:42
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!!!
Alex W. (Guest)
on 2007-02-10 09:06
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
Stewart (Guest)
on 2007-02-11 11:01

Hi Alex,

Thanks mate thats exactly what i was looking for.
Stewart (Guest)
on 2007-02-11 11:19
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
Stewart (Guest)
on 2007-02-12 09:04
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
Alan F. (Guest)
on 2007-02-12 16:16
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
Alex W. (Guest)
on 2007-02-12 19:45
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.
This topic is locked and can not be replied to.