I'm new to working with databases in Rails at this level, and I've looked for several hours but haven't found a solution to this specific problem.
Versions: Rails 3.2.9, Ruby 1.9.3, MySQL 5.5.28 (mysql2 gem 2.9.0), Mac OS 10.7.5
Error:
ActiveRecord::StatementInvalid in Action_figures#list
Mysql2::Error: Unknown column 'action_figures.position' in 'order clause': SELECT `action_figures`.* FROM `action_figures` ORDER BY action_figures.position ASC
Extracted source (around line #14): [list.html.erb]
11: <th>Visible</th>
12: <th>Actions</th>
13: </tr>
14: <% @action_figures.each do |action_figure| %>
15: <tr>
16: <td><%= action_figure.position %></td>
17: <td><%= action_figure.name %></td>
I generated the ActionFigures model and controller, but specified :position, :name, etc. later in the migration file:
class CreateActionFigures < ActiveRecord::Migration
def change
create_table :action_figures do |t|
t.string "name"
t.integer "position"
t.boolean "visible", :default => false
t.timestamps
end
end
end
And this in the model:
class ActionFigure < ActiveRecord::Base
has_many :pages
attr_accessible :name, :position, :visible
end
I've run rake db:migrate
several times already, stopped and restarted the server, closed and reopened Terminal just to be sure it wasn't those things, but when I do:
mysql> SHOW FIELDS FROM action_figures;
I get the following table:
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+----------+------+-----+---------+----------------+
Questions:
If you specified the position, name, and everything else in the old ActionFigures migration file db:migrate
won't pick up the changes in that file.
If you look at the schema.rb
file in your db folder it has a version number. That number matches up with the number on the migration file that was last run. So when you run rake db:migrate
it takes into consideration that number so it doesn't re-run the migrations before that. This happens so that tables don't try to get recreated and such...
You have a few options:
Do a rake db:rollback
to reverse the change of the creation of the ActionFigure table if you haven't done other migrations since.
Do rake db:drop
then rake db:create
and rake db:migrate
to recreate the table with the changes you made in the migration.
Delete the changes you made in the migration file and make a new migration using rails g migration add_name_and_position_to_action_figures name position:integer
and run rake db:migrate
to make the changes to your table.
There's other ways but from all I would just go with either 2 or 3 depending on the data you have on your database.
Hope this helps!
Did you add the table ... run the migrations .. and then edit the migration file to add position and visible attributes? If so, you'll need to add a new migration (or rerun the last migration)
Try this;
rails g migration add_position_to_action_figures position:integer visible:boolean
bundle exec rake db:migrate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.