On 3 February 2011 08:53, lorbrito
<lorenzo.brito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
You have to specify the foreign_key here if it is not restaurant_id.
I hope you are using non-standard keys because you are dealing with a
legacy database that you cannot change. If it is a new app your life
will be much easier if you stick to the rails conventions. Similarly
if it is a legacy db and you can change the schema then do it.
Colin
> 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?
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.