How can I search date range in Rails with variable date

后端 未结 4 1573
忘掉有多难
忘掉有多难 2020-12-17 16:33

How can I do this in rails active record¿?

find all models that match (created_at + 100 days between this month)

Edit: Ok, Sorry for not be precise this what

4条回答
  •  清酒与你
    2020-12-17 17:38

    You didn't specify Rails 2 or 3, and I'm not entirely sure what range you're actually looking for, but this should get you started. Please add some example dates and say whether they should fall into your range or not.

    In Rails 2 you can use a named_scope in your model.

    # This range represents "created_at" values that are within 100 days on either side of today.
    # Please clarify what "created_at + 100 days between this month" means if you need help
    # refining this.
    #
    named_scope :within_range, lambda {{ :conditions => ["created_at <= ? AND created_at >= ?", Date.today + 100, Date.today - 100] }}
    

    In Rails 3, you would use a scope with the new Arel scope methods:

    scope :within_range, lambda { where("created_at <= ? AND created_at >= ?", Date.today + 100, Date.today - 100) }
    

提交回复
热议问题