Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:36:42+00:00 2026-05-26T02:36:42+00:00

i want to add the available time when the user establish a vote .

  • 0

i want to add the “available time” when the user establish a vote . how to implement?

For example i set up a vote ,and it is available to vote by 2012.1.1.In addition ,the vote’s “available time” range from one day to one year.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:36:43+00:00Added an answer on May 26, 2026 at 2:36 am

    add a date column like “expires_at” and then you run a custom validation like:

    Solution A*

    if you have one single table called votings:

    id | name | votes | expires_at
    

    expires_at is a Date column

    Now your model looks like (voting.rb):

    class Voting < ActiveRecord::Base
      validate :check_expiry_date, :on => :update
    
      def check_expiry_date
        self.errors.add('base', 'Voting is closed') if self.expired?
      end
    
      def expired?
        self.expires_at < Date.today
      end
    end
    

    Now in your controller:

    @voting = Voting.find(someid)
    @voting.votes += 1
    
    if @voting.save
      # everyhing ok
    else
      # maybe the voting is closed, check validation messages
    end
    

    Solution B

    If you have a 2-Table approach like:

    Table Votings:

    id | name | expires_at
    

    Table Votes:

    id | user_id | voting_id
    

    You need two models:

    voting.rb

    class Voting < ActiveRecord::Base
      has_many :votes
    
      def expired?
        self.expires_at < Date.today
      end   
    end
    

    votes.rb

    class Vote < ActiveRecord::Base
      belongs_to :voting
      belongs_to :user
    
      # only one vote per user per voting
      validates_uniqueness_of :user_id, :scope => :voting_id
    
      # check expiry date
      validate :check_expiry_date, :on => :create
    
      def check_expiry_date
        self.errors.add('base', 'Voting is closed') if self.voting.expired?
      end
    end
    

    Your Controller:

    @vote = Vote.new
    @vote.user_id   = some_user_id
    @vote.voting_id = some_voting_id
    
    if @vote.save
      # everything ok
    else 
      # maybe the voting is closed
    end
    

    Creating a new Voting:

    @voting             = Voting.new
    @voting.name        = 'President Election 2011'
    @voting.expires_at  = 1.year.from_now
    @voting.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add copy and paste to my application but make it available
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to add a tab to a certain tab bar from inside the
I want to add some jQuery functionality to our sites where one piece of
I want to add new settings to the user.config at runtime for a C#
I want add my custom sidebar next right column all page. Please check this
I have a website built with ASP.NET (3.5) and want add some level of
I have a list of string values that I want add to a hashtable
I used RollingFileAppender. And I want add a blank line to the log when
I want to add a variable number of records in a table (days) And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.