models
class Restaurant < ActiveRecord::Base self.table_name = "restaurants" self.primary_key = "idrestaurant" #SELECT `comidas`.* FROM `comidas` WHERE (`comidas`.restaurant_id = 2) has_many :comidas, :foreign_key =>"idrestaurant" ,:primary_key=>'idrestaurant' has_many :comentarios ,:foreign_key =>"idrestaurant" end
class Comentario < ActiveRecord::Base self.table_name = "comentarios" self.primary_key = "idcomentarios" belongs_to :restaurant end controller_comentarios
def index @comentarios = Comentario.all
respond_to do |format| format.html # index.html.erb format.xml { render :xml => @comentarios } end end view comentarios index.html.erb
<h1>Listing comentarios</h1>
<table> <tr> <th>Restaurant</th> <th></th> <th></th> <th></th> </tr>
<% @comentarios.each do |comentario| %> <tr> <td><%= comentario.comentario_%></td> <td><%= comentario.estrellas %></td> <td><%= comentario.usuario %></td> <td><%= comentario.restaurant %></td> <td><%= comentario.idrestaurant %></td> <td> <%= Comentario.reflections.keys %> </td> <td> <%= comentario.restaurant.nill? %> </td> <=its says true for all, so the restaurant is nill but comentario.idresaturant show the correct value
<td><%= link_to 'Show', comentario %></td> <td><%= link_to 'Edit', edit_comentario_path(comentario) %></td> <td><%= link_to 'Destroy', comentario, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table>
<br />
<%= link_to 'New Comentario', new_comentario_path %> here is the code http://pastie.org/1524215 well formated. the question is that comentario.restaurant is nill, but its ok comentario.idrestaurant cause it has the right value. what im doing wrong? its suppose if i make @comentarios = Comentario.all, each element in @comentarios has to be a instance of restaurant not?