Brian Corrigan
2006-Mar-08 18:45 UTC
[Rails] Displaying related tables in forms.. probably an easy question!
Hey All, I have two tables.. One belongs to another. Why can''t I reference table1.table2.attribute? Specifically, using scaffolding: property.rb - has_one :PropertyType property_type.rb - has_many :Properties property_controller - def list @property_pages, @properties = paginate :properties, :per_page => 10 end list.html: 1. <% for property in @properties %> 2. <tr> 3. <% for column in Property.content_columns %> 4. <td> 5. <%=h property.send(column.name) %> 6. </td> 7. <% end %> 8. <td><%=h property.propertytype.name %></td> 9. <% end %> Two questions: 1) I get an error stating that I am trying to reference the propertytype method on line #8. I thought I was doing this right, I''m using the local variable (not the instance variable which is an array of objects in this case). 2) Am I doing my has_one and belongs_to statements right? Some documentation seems to say that you only need to use has_one and you don''t need belongs to? I thought you always needed them as a pair? - Brian
Nick Stuart
2006-Mar-08 21:15 UTC
[Rails] Displaying related tables in forms.. probably an easy question!
You just have your associations setup wrong. Property: belongs_to :property_type PropertyType: has_many :properties Try that and see how she flys! On 3/8/06, Brian Corrigan <brian.corrigan@mm-games.com> wrote:> > Hey All, > > I have two tables.. One belongs to another. Why can''t I reference > table1.table2.attribute? > > Specifically, using scaffolding: > property.rb - > has_one :PropertyType > > property_type.rb - > has_many :Properties > > property_controller - > def list > @property_pages, @properties = paginate :properties, :per_page => 10 > end > > list.html: > 1. <% for property in @properties %> > 2. <tr> > 3. <% for column in Property.content_columns %> > 4. <td> > 5. <%=h property.send(column.name) %> > 6. </td> > 7. <% end %> > 8. <td><%=h property.propertytype.name %></td> > 9. <% end %> > > Two questions: > 1) I get an error stating that I am trying to reference the propertytype > method on line #8. I thought I was doing this right, I''m using the > local variable (not the instance variable which is an array of objects > in this case). > 2) Am I doing my has_one and belongs_to statements right? Some > documentation seems to say that you only need to use has_one and you > don''t need belongs to? I thought you always needed them as a pair? > > - Brian > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060308/9fb6e613/attachment.html
Steve Koppelman
2006-Mar-08 21:59 UTC
[Rails] Re: Displaying related tables in forms.. probably an easy qu
The fact that you named property_type the way you did with an underscore makes it especially important that you understand how Rails deals with names. I''m not 100% sure, but try referencing property.property_type, with the underscore. Rails will know it as PropertyType and property_type depending on the context, but not as propertytype. Brian Corrigan wrote:> Hey All, > > I have two tables.. One belongs to another. Why can''t I reference > table1.table2.attribute? > > Specifically, using scaffolding: > property.rb - > has_one :PropertyType > > property_type.rb - > has_many :Properties > > property_controller - > def list > @property_pages, @properties = paginate :properties, :per_page => 10 > end > > list.html: > 1. <% for property in @properties %> > 2. <tr> > 3. <% for column in Property.content_columns %> > 4. <td> > 5. <%=h property.send(column.name) %> > 6. </td> > 7. <% end %> > 8. <td><%=h property.propertytype.name %></td> > 9. <% end %> > > Two questions: > 1) I get an error stating that I am trying to reference the propertytype > method on line #8. I thought I was doing this right, I''m using the > local variable (not the instance variable which is an array of objects > in this case). > 2) Am I doing my has_one and belongs_to statements right? Some > documentation seems to say that you only need to use has_one and you > don''t need belongs to? I thought you always needed them as a pair? > > - Brian-- Posted via http://www.ruby-forum.com/.