Simple Rails Application trouble

Thread Solved

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Simple Rails Application trouble

 
0
  #1
Dec 31st, 2007
Hey everyone,

I am new to Ruby on Rails and I am having a problem that I can't seem to figure out. I have a mysql database that I created and I have created a model for it. I have did not create a migration, as the database is already full of information. I added a scaffold to the application, and it showed the data from the database with no problems.

I have tried to create a new controller called portfolio, and in this I have an index. Here is the controller code:

  1. class PortfolioController < ApplicationController
  2. def index
  3. @stocks = Stock.find_active_symbols
  4. end
  5. end

The @stocks is, from what I understand, supposed to contain the rows returned from the database. Below is my Stock Model code:

  1. class Stock < ActiveRecord::Base
  2.  
  3. def self.find_active_symbols
  4. find(:all, :order => "stock_symbol")
  5. end
  6. end

Finally, I have a view created in app/views/portfolio/index.rhtml Below is the code from there:

  1. <% for st in @stocks -%>
  2. <%= h(st.stock_symbol) %>
  3. <% end %>

in the database table, I have a column named stock_symbol. When I try and access this view in a web browser, I get the following error message:

  1. undefined method `stock_symbol' for #<Stock:0xb72e11a4>
  2.  

I don't understand why I am seeing this. Anyone have any ideas?

Thanks for the help in advance.

Nick
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 521
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: Simple Rails Application trouble

 
0
  #2
Jan 1st, 2008
Looking at your code I can't see whats wrong.

What happens if in your view you do

  1. <% for st in @stocks -%>
  2. <%= h(st.inspect) %><br />
  3. <% end %>

It may give a clue as to what's wrong.
Note to self... pocket cup
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: eruder is an unknown quantity at this point 
Solved Threads: 1
eruder eruder is offline Offline
Newbie Poster

Re: Simple Rails Application trouble

 
0
  #3
Jan 1st, 2008
Since you're working with a MySQL database that's already full of data, do they follow Rails conventions? I.e., are the table names plural (e.g., the table corresponding to the Stock model should be named "stocks") and does each table with a Rails model class have an "id" field that's its primary key?

If not, that would cause problems (though I'm not sure if it would cause the problem you're seeing at the moment).

Ed
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Simple Rails Application trouble

 
0
  #4
Jan 2nd, 2008
Eruder,

I believe that is the problem. I added a column called id and set it as the primary key and now it seems to work fine. Thank you for your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC