views:

49

answers:

2

Table City Model

class TableCity < ActiveRecord::Base
  has_many :near_cities, :foreign_key => "nearcity_id"  
end

NearCity Model

class NearCity < ActiveRecord::Base
  belongs_to :table_city
end

Controller

@table_cities = TableCity.find(:all, :conditions=>{:state => params[:gm], :language => params[:sp]}, :include => :near_cities)

View

<% @table_cities.each do |table_city| %>
  <%= table_city.name %>
  <% table_city.near_cities.each do |city| %>        
    <%= city.text %>
  <% end %>   
<% end %>   

This is not working, Kindly give me some suggestions.

+2  A: 

Your relationships look strange. A NearCity belongs to a TableCity therefore, you should be storing the foreign key of the TableCity in your near_cities table. To achieve this make sure your near_cities table has a column called table_city_id.

Your models can simply be:

class TableCity < ActiveRecord::Base
  has_many :near_cities  
end

class NearCity < ActiveRecord::Base
  belongs_to :table_city
end
captaintokyo
Thank you for kind help. If I need to set foreign_key as a colulmn which dont have suffix "id". I need to mention that, In that case what i can do.
Palani Kannan
In that case you have to use `:foreign_key => `. For example: `belongs_to :table_city, :foreign_key => 'nearcity'`.
captaintokyo
@captaintokyo: :foreign_key => 'other_id'... Not works, No error... Its nor connecting the data of belongs table_city.
Palani Kannan
class TaxonomicLink < ActiveRecord::Base belongs_to :taxon_name class TaxonName < ActiveRecord::Base has_many :taxonomic_links, :foreign_key => 'name_id' @taxon_names = TaxonName.find(:all, :conditions=>{:genus_name => params[:gm], :corrig => params[:ck1], :type_species => params[:ck2], :sp_epithet => params[:sp]}) <% @taxon_names.each do |taxon_name| %><%= taxon_name.genus_name %> <% taxon_name.taxonomic_links.each do |tax_link| %><%= tax_link.text %><% end %><% end %>
Palani Kannan
You are putting `:foreign_key` in the wrong place! Do it like this: `class TaxonomicLink < ActiveRecord::Base belongs_to :taxon_name, :foreign_key => 'name_id'` and make sure your `taxonomic_links` table has a column called `name_id`.
captaintokyo
If I use :foreign_key as attribute in belongs_to, It shows "ActiveRecord::StatementInvalid". It shows none error if i use it as attribute for has_many. I am using Rails 3.0.0
Palani Kannan
Once I provided primary_key of taxon_names, it connects and working fine. But as hard part, I dont know the hidden reason behind the "primary key". No other forums and tutorials, i didnt read abt role of primary key in connecting tables...
Palani Kannan
I am curious which code exactly you are using now. Could you post it?? `primary_key` is used if the primary key column in your table is something other than `id`.
captaintokyo
I found the reason. The foreign key was not declared before during migration. Because, I do manual migration. The foreign key of taxonomic_links == primary key of taxon_names. Thats, It requires primary key of taxon_name to connect with foreign_key of taxonomic-Links.
Palani Kannan
A: 

In your Models

class TableCity < ActiveRecord::Base
  has_many :near_cities  
end

class NearCity < ActiveRecord::Base
  belongs_to :table_city
end

In your migrations

class CreateTableCities < ActiveRecord::Migration
  def self.up
    create_table :table_cities do |t|
      t.string :name
      t.timestamps
    end
  end

  def self.down
    drop_table :table_cities
  end
end

class CreateNearCities < ActiveRecord::Migration
  def self.up
    create_table :near_cities do |t|
      t.string :name
      t.references :table_city
      t.timestamps
    end
  end

  def self.down
    drop_table :near_cities
  end
end

And then finally in your controller you can do this

@table_city = TableCity.find(params[:id]) #one TableCity
@near_cities = @table_city.near_cities    # all near_cities associated with the TableCity
Rohit