I have 2 models, User and Email, As it's clear a user will have an email, as sender and recipient.
In User model
has_many :sent_emails, class_name: "Email", foreign_key: "sender_id"
has_many :received_emails, class_name: "Email", foreign_key: "recipient_id"
and in Email model
belongs_to :sender, class_name: "User", primary_key: "id"
belongs_to :recipient, class_name: "User", primary_key: "id"
That's how am getting associations, That's was for information only, The problem is: I have other 2 columns as cc and bcc as
add_column :emails, :cc, :integer, default: [], array: true
add_column :emails, :bcc, :integer, default: [], array: true
I want to make associations between email and user on those columns as well.
So That if I add,
=> #<Email id: nil, sender_id: 5, recipient_id: 10, body: "Hello its first email.", subject: "Hello Hello", state: 1, head: nil, tail: nil, created_at: nil, updated_at: nil, cc: [9, 8], bcc: [4,19]>
As I can do Email.first.sender or Email.first.recipient I can get the user, So that I may do Email.first.cc or Email.first.bcc I can get the users.
As I can do User.first.sent_emails and User.first.received_emails, I can get User.first.cc_mails or User.first.bcc_mails?
Is that possible to build association like that?