Get the object caller

Let me explain

I have these models

class User < AR::Base
has_many :contact
end

class Contact < AR::Base
belongs_to :user
belongs_to :user_contact_id, :class_name => “User”, :foreign_key =>
“user_contact_id” # I have field called :user_contact_id who is the
another user record of the user table.

def self.is_contact?(user_contact_id)
# CHECK IF THE USER ALREADY HAVE A CONTACT WITH THE USER_CONTACT_ID

end
end

I’ve an instantiated User class, @user, and want to do the following

@user.contacts.is_contact?(user_contact_id) # This works like a charm.

My question is, how can i access the @user caller object from the
self.is_contact? method in Contact Class? Is this possible?

Thanks.

@user.contacts.is_contact?(user_contact_id) # This works like a charm
It does? What’s the content of the “is_contract?” function?

It might be easier to help if you tell us what you’re trying to achieve.
What are you going to do once you know if the user have / doesn’t have a
contract with that user_contract_id?

On Feb 5, 3:22 pm, “Augusto Ruibal (Shinta)”
[email protected] wrote:
e an instantiated User class, @user, and want to do the following

@user.contacts.is_contact?(user_contact_id) # This works like a charm.

My question is, how can i access the @user caller object from the
self.is_contact? method in Contact Class? Is this possible?

you can’t. What do you need it for ? - any find you do on Contacts
from inside is_contact? will be magically scoped to @user’s contacts
anyway.

Fred

On Feb 5, 10:22 am, “Augusto Ruibal (Shinta)”
[email protected] wrote:

    belongs_to :user_contact_id, :class_name => "User", :foreign_key =>

@user.contacts.is_contact?(user_contact_id) # This works like a charm.

A plain :through could do what you’re looking for:

class Contact < AR::Base
belongs_to :user
belongs_to :user_contact, :class_name => “User”
end

class User < AR::Base
has_many :contacts
has_many :user_contacts, :through => :contacts
end

(you may need a :source and/or :class_name on :user_contacts, try it
and see)

Then you can do:

@user.user_contacts.include?(some_other_user)

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs