Tim K.
Tim K.

Reputation: 170

Find most recent donation excluding funds beginning with X

I'm trying to grab the gift date of the most recent donation (in $USD) to my organization that has a fund ID not beginning in X (those are Canadian donations). Here's the code (in the controller) that I've used to pull out the gift date of the most recent overall donation, but it's a Canadian gift (fund_id = XP05). How do I get the most recent donation with a fund ID not beginning with an X?

@income_batch = Gift.between(Date.today - 2.weeks, Date.today, :field => :gift_date).order('gift_date DESC').first.gift_date

Upvotes: 0

Views: 55

Answers (1)

user229044
user229044

Reputation: 239291

Assuming fund_id is a string field within your gifts table, you can use not like:

Gift.where("fund_id not like 'X%").between....

You should also avoid using an _id postfix on fields unless they're foreign keys, as Rails convention dictates that _id is specific for this purpose.


If fund_id is a foreign key, and you have a belongs_to :fund inside your Gift model, you can use joins and not like:

Gift.joins(:fund).where("funds.name not like 'X%").between....

This assumes that the field within the funds table is called name.

Upvotes: 1

Related Questions