I currently have a User (has_many: address_book_users) and an AddressBookuser (columns: user_id, user_id_inserted)
Currently, when I call user.address_book_users, I get back an array on ints, like [3, 4, 5] which correspond to users which are in my address book.
When iterating through this array I have to do a:
for uid in user.address_book_users
user = User.find(uid)
to get access to each user.
What would be a better way to do this, or should I remodel my relationship somehow to where Rails knows that column user_id_inserted refers to an ID of a user?
First you have to name the
AddressBookUsermodel’suser_id_insertedlike, contact.Then define a has_many :through relation.
Then call
user.contactsand you should receive all the users on addressbook.