How get model by double relation without using like where(id: @user.id)

24 views
Skip to first unread message

Николай Спелый

unread,
Sep 7, 2015, 6:48:50 PM9/7/15
to Ruby on Rails: Talk
This strange title is all that i think out.

I have to objects: @user (instance of User model) and @chat (instance of Chat model).

And i need to get record from table chats_users (relation has name chatusers) by these having objects, not by one of them and writing something like this: @user.chatusers.where(chat_id: @chat.id) or @chat.chatusers.where(user_id: @user.id).

What i looking foor must looking like this:

@user.double_relation(@chat, target: chatusers)

Thanks.

Frederick Cheung

unread,
Sep 8, 2015, 6:43:10 AM9/8/15
to Ruby on Rails: Talk
There is no such thing -  you will have to write such a method yourself (which would just do chatusers.where(chat_id: chat) internally, assuming this is a method on user)

Fred
Reply all
Reply to author
Forward
0 new messages