Rails Seed Data not loading properly after migration - ruby-on-rails-3

I loaded some seed data in using the code below, and it worked fine. Then, I needed to add two more columns, and I did so using the following steps, but it's not attaching the two new columns seed data to the table, what am I doing wrong?
Steps:
Add two columns using migration
Make them attr_accessible in the model
Replace old CSV file with new CSV file
Change the seed.rb file to plug in the new data
Run rake db:seed
Seed.rb
require 'csv'
Model.delete_all
CSV.foreach("#{Rails.root}/lib/data/model.csv") do |row|
Model.create!(:model_number => row[0], :areq => row[1], :length => row[2], :width => row[3], :depth => row[4], :material => row[5], :frame => row[6], :edge => row[7], :tubes => row[8], :tube_length => row[9])
end
Schema.rb
create_table "models", :force => true do |t|
t.string "model_number"
t.float "areq"
t.float "length"
t.float "width"
t.float "depth"
t.string "material"
t.string "frame"
t.float "edge"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.float "tubes"
t.float "tube_length"
end

Cant say this may work 100%
Try Model.reset_column_information above the seed file once
this should refresh the columns information in table.
require 'csv'
Model.reset_column_information
Model.delete_all
#......
just a guess check if the proper model.csv is getting loaded and have column 8 and 9 means have in all 10 columns.

Related

Rails: Trouble With Model Association Using Ancestry Gem (Tree Structur Model)

I am working on a simple app that allows a user to write a post (parent_post), and another user to answer this post (child_post).
I am using the Ancestry gem to track the relationships between the posts.
Ancestry is a gem/plugin that allows the records of a model to be organised as a tree structure (or hierarchy). It exposes all the standard tree structure relations (parent, root, children, ancestors...).
The database schema.rb of the app:
create_table "posts", :force => true do |t|
t.text "title"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.string "ancestry"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
end
The challenge:
I want to display informations about the author of the parent_post in the show view of a child_post .
Via the Ancestry gem you can call for "parent" and address all the columns inside of the posts table.
The posts table (see above) has a user_id column, so I can call
#post.parent.user_id
to show me the User ID, which works.
But I would like to show the username instead of the user_id.
Of course, user_id and username (name) are via the users table connected (see schema.rb above), but how can I address them here?
#post.parent.user_id.name is not working.
The models:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
has_ancestry
belongs_to :user
end
I am still quite new to Rails and stuck here. Is there maybe a super easy solution I am not seeing?
Thank you so much for helping out!
How about #post.parent.user.name? I recommend making this a method on the Post model rather than your view or controller. Call it #post.original_poster_name or similar.

ActsAsTaggableOn after own tag implementation

I had my own implementation of tags in my rails application and wanted to replace it by the ActsAsTaggableOn gem. I generated the migration and deleted all the migrations with tags in it, but forgot to rollback first, so I just did rake db:reset. Now the schema looks fine with tag and taggable in it, but if I try Tags out in the Console, by just typing Tag i get
NameError: uninitialized constant Tag
What causes this error, I thought I just did like in the Railscast.
My schema.rb looks like this:
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121009203921) do
create_table "comments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "message_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "events", :force => true do |t|
t.string "name"
t.datetime "start_at"
t.datetime "end_at"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "messages", :force => true do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", :limit => 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", :force => true do |t|
t.string "name"
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "encrypted_password"
t.string "salt"
t.boolean "admin", :default => false
t.string "phone"
t.string "street"
t.string "zip"
t.string "location"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
end
Thanks for your help!
I had to have a model, to get it work in the console.

rails add_column with default value but the default value don't take effect

My rails version is 3.2.8 and use the default database.
This is my migration code:
class AddQuantityToLineItem < ActiveRecord::Migration
def change
add_column :line_items, :quantity, :integer,:default=>1
end
end
I find a explaination about :default option here and as it said,when i create a
new LineItem ,it should have a default quantity=1,but here is what i get from rails console:
lineb=LineItem.new
#<LineItem id: nil, product_id: nil, cart_id: nil, created_at: nil, updated_at: nil, quantity: nil>
And when i get LineItem from the database,the quantity field is nil
too.
And here is the db/schema.rb :
ActiveRecord::Schema.define(:version => 20121008065102) do
create_table "carts", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "line_items", :force => true do |t|
t.integer "product_id"
t.integer "cart_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "quantity"
end
create_table "products", :force => true do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.decimal "price", :precision => 8, :scale => 2
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
Your migration should work fine. Based on your schema though it looks like it hasn't actually taken effect, since t.integer "quantity" has no default value.
The line in the schema for quantity should look like this:
t.integer "quantity", :default => 1
Make sure you have actually run your migration (bundle exec rake db:migrate), and if that doesn't work then rollback (bundle exec rake db:rollback) and run the migration again (as #surase.prasad suggested).

Rails Migration to make a column null => true

I had originally created a table with column as
t.string "email", :default => "", :null => false
The requirement has changed and now I need to allow email to be null. How can I write a migration to make :null => true
change_column_null worked perfectly:
change_column_null :users, :email, true
The reverse has a nice option to update existing records (but not set the default) when null is not allowed.
Try:
change_column :table_name, :email, :string, null: true

Having issues importing a CSV File using FasterCSV

I am new to Rails and I have figured out how to export results from my Database but I am having issues creating new records from a CSV File. With the code listed below I want to be able to import a CSV file and fill in the last two columns with session data from the user. For now I just inserted a static number to try to get this working. I currently receive "can't convert ActionDispatch::Http::UploadedFile into String" as the error message
CSV DOCUMENT
name,task,expected_results,require_id
Test Name 1,Open a File,Open,t
Test Name 2,Read a File,Read,t
Test Name 3,Close a File,Close,f
CONTROLLER
def csv_import
file = params[:file]
FasterCSV.foreach(file,{:headers => true, :row_sep => :auto}) do |row|
Script.create!(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
end
VIEW
<%=form_tag '/script_admin/csv_import', :multipart => true do%>
<%= file_field_tag "file" %><br/>
<%= submit_tag("Import") %>
<% end %>
DB MIGRATION
class CreateScripts < ActiveRecord::Migration
def self.up
create_table :scripts do |t|
t.integer :department_id, :null => false
t.integer :category_id, :null => false
t.string :name, :null => false
t.string :task, :null => false
t.string :expected_results, :null => false
t.boolean :require_id, :null => false, :default => "t"
t.timestamps
end
end
def self.down
drop_table :scripts
end
end
Any help with this can be appreciated
~Kyle
Issue is resolved thanks to James Gray
def csv_import
file = params[:file]
FCSV.new(file.tempfile, :headers => true).each do |row|
Script.create!(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
end