has_many :through

I have the 3 tables

1)service_desk_tickets

id integer not null default
nextval('public.service_desk_tickets_id_seq':
:text)
number character varying(15)
title character varying(100)
service_desk_status_id integer
etc.....

2)cis

id integer not null default nextval('public.cis_id_seq'::text)
citype character varying(30)
ci_number character varying(15)
content_id integer
etc....

3)service_desk_cis(This is a junction table)

id integer not null default nextval('public.service_desk_cis_id_se
q'::text)
service_desk_ticket_id integer
ci_id integer
service_desk_ci_association_type_id integer

Associations as
In service_desk_ticket.rb

has_many :service_desk_cis
has_many :cis, :through => :service_desk_cis

In ci.rb
has_many :service_desk_cis
has_many :service_desk_tickets, :through => :service_desk_cis

in service_desk_cis.rb

belongs_to :service_desk_ticket
belongs_to :ci

In controller
@sd_cis=ServiceDeskTicket.find(params[:id]).cis (This is working)

MY PROBLEM IS HOW TO GET service_desk_ci_association_type_id

Please help
Sijo

Hi,

The implementation of has_many and belongs_to association is only not enough to join two taIf u have implement two tables…And also u use the following query

         @sd_cis=ServiceDeskTicket.find(params[:id], :include [:service_desk_cis])

         and u retrieve the field of ServiceDeskTicket table as
        
         @sd_cis.id
        
          and u retrieve the field of Service_desk_cis table as
         @sd_cis.service_desk_cis.service_desk_ci_association_type_id

I did
in controller

@sd_cis=ServiceDeskTicket.find(params[:id], :include =>
[:service_desk_cis]).cis

And in view

<% 0.upto(@sd_cis.length-1) do |loop_index| %>

      <td><%=CiStatus.find(@sd_cis[loop_index].content.ci_status_id).name%></td>
      <td><%=@sd_cis[loop_index].ci_number%></td>
      <td><%=CiType.find(@sd_cis[loop_index].citype).name%></td>
      <td><%=@sd_cis[loop_index].content.name%></td>
      <td><%=CiClass.find(@sd_cis[loop_index].content.ci_class).name%></td>
      <td><%=
@sd_cis[loop_index].service_desk_cis.service_desk_ci_association_type_id
%></td>

    <% end %>

If I comment last line(@sd_cis[loop_index.....)No error everything
ok.But if uncomment it I get the error
undefined method `service_desk_ci_association_type_id' for
ServiceDeskCi:Class

Sijo

Hi,

U may try this type of for loop tag to display the contents…

 <% for sd_cis in @sd_cis %>

<%= sd_cis.service_desk_cis.service_desk_ci_association_type_id %>

<% end %>

I think i should helpful to u…

Did you create foreign key in your migration when you created
service_desk_cis to its related table ?

I see your migration file. you dont have foreign key to link them in
database, you should add like this in your new migration :

execute 'ALTER TABLE service_desk_cis ADD CONSTRAINT
fk_service_desk_cis
    FOREIGN KEY (service_desk_status_id) REFERENCES
service_desk_tickets(id)'

I hope it will works.

The servicedeskticket migration was

class CreateServiceDeskTickets < ActiveRecord::Migration
  def self.up
    create_table :service_desk_tickets do |t|
      t.column :number, :string, :limit=>15
      t.column :title, :string, :limit=>100
      t.column :status, :integer
      t.column :category, :integer
      t.column :sub_category, :integer
      t.column :primary_assignee_group, :string, :limit=>50
      t.column :impact, :integer
      t.column :urgency, :integer
      t.column :priority, :integer
      t.column :resolved, :boolean, :default=>false
      t.column :description, :string, :limit=>100

    end
  end

  def self.down
    drop_table :service_desk_tickets
  end
end

In the controller
@sd_ticket=ServiceDeskTicket.find(params[:id])
@sd_ticket.service_desk_cis.each do |t|
          puts t.service_desk_ci_association_type_id
        end

This worked..What may be the differnce?

sorry, my internet was down.

the difference is :

-- YOUR PROBLEM HERE ---
@sd_cis=ServiceDeskTicket.find(params[:id], :include
=>[:service_desk_cis]).cis

-- YOUR WORKING TEST SCRIPT ---
@sd_ticket=ServiceDeskTicket.find(params[:id])

it's quite difference. Please run it :

@testcis = Ci.find(params[:id])
@testcis.service_desk_cis.service_desk_ticket_id

I need to know what it works or no. if it is not working, it means that
your cis can not be linked to service_desk_cis, so you can try to add
foreign key there.

Thanks for your reply..It worked like

@servicedesk_cis_join = @sd_ticket.service_desk_cis
@sd_cis = @servicedesk_cis_join.map(&:ci)
@sd_cis_association_type_ids =
servicedesk_cis_join.map(&:service_desk_ci_association_type_id)

And in view

<%= @sd_cis_association_type_ids[loop_index] %>