You can use the following (Rails 3):
User.where(invited_by_id: current_inviter).last
Or use the old syntax (Rails 2):
User.find(:last, :conditions => ['invited_by_id = ?', current_inviter])
Question
It's probably unrelated to the issue, but for context I'm using the devise_invitable gem in my app. I've started to modify the invitations_controller.rb as I want to save additional associations between the invited_user
and other models in my app.
I think the root of my issue is that an inviting_user
can invite multiple other users and each of these associations is saved in the User model with a field called invited_by_id
Before making the additional desired associations between the invited_user
and my other models, I retrieve the invited_user's id
with the following:
@invited_user = User.find_by_invited_by_id(current_inviter)
This does successfully retrieve a record from the User table, but it appears to be the first user invited by the current_inviter
.
I'd like to change the logic so it retrieves the last user invited by the current_inviter
. As such, I tried:
@invited_user = User.find_by_invited_by_id(:last, current_inviter)
But unfortunately that returns the following error:
NoMethodError: undefined method `empty?' for 1:Fixnum
Any assistance is appreciated. Thank you.
Solution
You can use the following (Rails 3):
User.where(invited_by_id: current_inviter).last
Or use the old syntax (Rails 2):
User.find(:last, :conditions => ['invited_by_id = ?', current_inviter])