Doubt in joins condition?

hi

I have two tables agencies contracts

i have one statement like below

@getdata=Agency.all(:joins=>:contracts, :select=>“agencies.*”,:conditions=>[“agencies.status =?”,1 ])

i am able to access all data in agency but not able to access contracts data from above query

please guide me how to do the above it is very critical

Hi Sijo

It has the below relation

has_and_belongs_to_many :contracts has_and_belongs_to_many :agencies

Hi Karthik Kantharaj

Ok One more thing please tell what you are trying to attain with the above query?

Sijo

Hi sijo

With the below query

@getdata=Agency.all(:joins=>:contracts, :select=>“agencies.*”,:conditions=>[“agencies.status =?”,1 ])

I want to access and retrieve the data from both tables

condition both table id should be equal

e.g agencies.id=contracts.id

In the below method

for getdata in @getdata do puts getdata.name end

I am able to access agencies data but not contracts data I need to know how to access contracts data

because i need to group them, one contract can have any number of agencies

contract1 agency1 agency2 contract2 agency1 agency2

I have explained you my exact requirement sijo, i am stuck in this i need help

Thanks

Hi    you get agencies for each contract like

Contract.all.each { |c| c.agencies }

    Here you get each contract information too For example

Contract.all.each do |c|   c.name #contract name   c.agencies end

Can collect it to an array like

array = Contract.all.each do |c| array << [c,c.agencies] end     and can iterate the array now

Sijo

Hi Sijo

Thank you In the below code

Contract.all.each do |c|

c.name #contract name

c.agencies

end

i am not able to get c.agencies.name I have name in agencies table but i am not able to

help me Plz

Hi Karthik

  For example consider this

data_array = Contract.all.each do |c| data_array << [c,c.agencies] end

    Now in view you can iterate thru this like

data_array.each do |arr| puts arr[0].name arr[1].each do |sub_arr|   puts sub_arr.name end end

        You can modify this according to your needs May be there some other easier methods

Sijo

Hi Sijo

Thank you Thank you

Great Thank you

hi sijo

I did and got the output

<% @data_array.each do |arr|%>
           <% if arr[1].length>0%>              
              <th><%=  arr[0].name%></th>
            <%end%>
            </tr>
            <%for sub_arr in arr[1]%>

          <tr>
              <td><%= [sub_arr.name](http://sub_arr.name)%></td>
            
          </tr>
          
          <%end%>

<%end%>

for the above table i am getting output as

e.g contract1 agency1 agency2 contract2 agency1 agency2

Can i get in the below format

contract1 agency1 agency2

contract2 agency1 agency2 agency3

I dont know how to split from <%for sub_arr in arr[1]%>

Plz help