I had a problem with an ActiveRecord request in Rails 4:
my models:
class Addressee < ActiveRecord::Base
has_and_belongs_to_many :emails
end
class Email < ActiveRecord::Base
belongs_to :author, foreign_key: :from_id, class_name: "Addressee"
has_and_belongs_to_many :addressees
end
my schema.rb
create_table "addressees", force: true do |t|
t.string "token", limit: nil
t.string "domain", limit: nil
t.string "email", limit: nil
t.string "name", limit: nil
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "addressees_emails", force: true do |t|
t.integer "addressee_id"
t.integer "email_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "emails", force: true do |t|
t.string "message_id", limit: nil
t.integer "from_id"
t.string "subject", limit: nil
t.text "body"
t.datetime "date"
t.datetime "created_at"
t.datetime "updated_at"
end
my request:
query_params = { "addressees.email" => "test@example.com" }
@emails = Email.includes(:addressees).where(query_params).references(:addressees)
my problem:
@emails.last.addressees
@emails.last.addressees.size
@emails.last.addressees.count
my question is:
How can I modify a request to include all recipients without requiring another request ?. I pass @emails var to the json serializer and now it only includes 1 destination instead of all 3.
source
share