On Sun, Jul 24, 2011 at 6:05 AM, Conrad Taylor <[email protected]> wrote:

> On Sat, Jul 23, 2011 at 9:56 PM, Rodrigo Ruiz <[email protected]>wrote:
>
>> Hi, Id like to do something like that:
>>
>> @payment_history = PaymentHistory.where("company_id = #{@company.id} AND
>> created_at <= #{Time.now} AND create_at >= #{Time.now - 1.month}").first
>>
>
> Rodrigo, you might be able to do something like this:
>
> time_range = (Time.now - 1.month)..(Time.now)
> @payment_history = PaymentHistory.where( :company_id => @company.id ).where(
> :created_at => time_range ).first
>
>
If the following is the case:

class Company
  has_many :payment_histories

  def first_paymeny

end

class PaymentHistory
   belongs_to :company
end

Then you should write something like the following:

paymant_histories = []

unless @company.nil?
  time_range = (Time.now - 1.month)..(Time.now)
  paymant_histories = @company.payment_histories.where( :created_at =>
time_range )
end

@payment_history = paymant_histories.first

In the above, we scoped the PaymentHistory from the context of the Company
instance
and this makes our intentions clear.  Next, payment_histories will always
return an array.
Thus, you'll have no worries of invoking the first method on it and it will
not require using
the try method.

Good luck,

-Conrad

Good luck,
>
> -Conrad
>
>
>>
>> problem is it doesn't seen to let me compare the create_at attribute with
>> Time.now.
>>
>> Anyone knows the proper way to do this?
>>
>> Thank you,
>> Rodrigo
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/rubyonrails-talk?hl=en.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to