mardi 29 novembre 2016

How to use joint query in this association - Ruby on Rails

I am working in ruby 2.1.5 and rails 3.2.1. I want to list all the company in grid which is associated to company_name = John

Company table:

enter image description here

company model:

has_many :partner_relationships, :class_name => "CompanyRelationship",:foreign_key => 'parent_id',

company_relationships table:

enter image description here

I want to get all the company information from company table where company.id = partner_id. I tried the below query

Company.joins(:partner_relationships).where('company_relationships.parent_id' => company.id)

This is returning 3 set of same data that is <#id:2, company_name:John, description:I am John#>

I want to return the records as follows <#id:1, company_name:Jose, description:I am Jose#>, <#id:3, company_name:George, description:I am George#>,..<#id:5, company_name:Alwin, description:''#>

Please help me in solving this.

Aucun commentaire:

Enregistrer un commentaire