开发者

is their any date filter in ruby?

开发者 https://www.devze.com 2023-04-12 21:49 出处:网络
1) i have a leave request form in my HRM application, i want that when a user enter the \"from_date\", the \"to_date\" should be greater than \"from_that\"?

1) i have a leave request form in my HRM application, i want that when a user enter the "from_date", the "to_date" should be greater than "from_that"?

2) In my app i have a leave model in which user can defined leave type and day's allowed.In my second model leave_request user can request for leave according to leave type.Everything is working well.Now i want to display leave balance available to user on basis of allowed day's in leave_request form?

Migration leave.rb:

  class CreateLeaves < ActiveRecord::Migration
      def self.up
        create_table :leaves do |t|
          t.integer :user_id
          t.integer :company_id
          t.string  :leave_type
          t.integer :allowed_leaves
          t.text :description, :limit => 500

          t.timestamps
        end
      end

      def self.down
        drop_table :leaves
      end
    end

And leave_request.rb:

 class CreateLeaveRequests < ActiveRecord::Migration
      def self.up
        create_table :leave_requests do |t|
          t.integer :employee_id
          t.integer :lea开发者_StackOverflowve_type
          t.date :from_date
          t.date :to_date
          t.text :reason_for_leave
          t.string :contact_during_leave, :limit => 10

          t.integer :user_id
          t.integer :company_id

          t.timestamps
        end
      end

      def self.down
        drop_table :leave_requests
      end
    end

And can i validate number of leaves available like if user do not have enough leave balance than their should be a message like"You do not have enough leave to request " etc.


Your first question is not very comprehensible. What is "from_that"? If you're asking how to check if one date is before or after another, it's easy: Date and DateTime include Comparable so you can compare any two just like you would two numbers, e.g.:

>> DateTime.now > 1.day.ago
# => true

>> DateTime.now > Date.tomorrow
# => false


You can write your own validator, something like this (untested code):

class LeaveRequest < ActiveRecord::Base
    validate :has_enough_leave

    def has_enough_leave
       if( to_date - from_date > employee.allowed_leave.days )
           errors.add(:to_date, "You do not have enough leave to requestt")
       end
    end

    ...


I have done my first problem in following way:

class LeaveRequest < ActiveRecord::Base
      belongs_to :user
      belongs_to :leave

    validate :from_date_and_to_date

         def from_date_and_to_date
           if(to_date != from_date)
             errors.add(:to_date, " should be greater than from date" )
           end 
         end


Hi friends here i came with a new answer of my second question

i used following line of code to fix my problem:

<label><a href="/leave_types">Leave Type</a></label>
  <label>Balance</label>

 <% for leave in @leave_types %> 
   <%= leave.leave_type %>
   <%= leave.allowed_leaves%>
 <% end %>

This displayed all my leave_types in my new leave_request form.

0

精彩评论

暂无评论...
验证码 换一张
取 消