unable to create an entry with chinese characters in rails console - ruby-on-rails

I am creating a platform for chinese students in ruby on rails. This is my schema
ActiveRecord::Schema.define(version: 20130827203308) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "posts", force: true do |t|
t.text "message"
t.integer "student_id"
end
add_index "posts", ["student_id"], name: "index_posts_on_student_id", using: :btree
create_table "students", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "number"
t.string "college"
t.string "password"
t.float "budget"
t.string "picture"
t.datetime "created_at"
t.datetime "updated_at"
end
when I head to the rails console and do this
Post.create(:message => "I am looking at a 3 bedroom apartment")
I can Begin and Commit this into my db.
But when I do
Post.create(:message => "我在Suffolk University附近想租一个3房一厅的apt有没有同学有兴趣“)
my rails console freeze.
I am using the type "text" for my messages in the posts table. Should I be using "string" ? or should I be using some sort of a language gem to do this?

Try installing readline package with rvm and recompile your ruby:
$ rvm pkg install readline
$ rvm install <your_ruby_version>

Related

How to add primary key to Rails?

I'm trying to make an e-commerce website type thing using Rails. So I've made my models for it. My problem is how to make a particular element a primary key?
create_table "bookmarks", :primary_key => bk_id force: :cascade do |t|
t.string "bk_name"
t.string "size"
t.string "brand"
t.string "product_id"
t.integer "mrp"
t.text "colour"
t.integer "stock"
t.integer "discount"
t.text "bk_description"
t.integer "bk_id", primary:true
t.integer "cart_unit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
This is a portion of the schema.rb file. Is the way I've marked bookmark id as the primary key correct? Also, after making these changes, I ran rails db:migrate command and the primary key portion disappears and it becomes like this-
create_table "bookmarks",force: :cascade do |t|
t.string "bk_name"
t.string "size"
t.string "brand"
t.string "product_id"
t.integer "mrp"
t.text "colour"
t.integer "stock"
t.integer "discount"
t.text "bk_description"
t.integer "bk_id"
t.integer "cart_unit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
I don't understand why this happened and why those changes I made (I did save the file after editing) just disappeared. Can someone explain this to me? Also, I'd like to mention that I'm learning Ruby on Rails so...please be gentle with me. :P
In your migration file:
create_table :bookmarks, :primary_key => :bk_id do |t|
...
t.integer :bk_id
...
end
Do not forget to indicate it in your Model too:
class Bookmarks < ActiveRecord::Base
self.primary_key = 'bk_id'
end
Assuming it’s Rails4+, you might do:
create_table :bookmarks, force: :cascade do |t|
...
t.integer :bk_id, primary_key: true
...
end
In Rails3 you just put an additional statement after:
create_table "bookmarks", force: :cascade do |t|
...
t.integer "bk_id"
...
end
execute "ALTER TABLE bookmarks ADD PRIMARY KEY (bk_id);"
Don't change content of schema.rb file. This content will be auto generated from your migrate files. Try find your create bookmarks migrate file and add :primary_key => bk_id to it.
File: db/migrate/xxxxxxxxxx_create_bookmarks.rb
(xxxxxxxxx is a timestamp)
Help it helps.

Error trying to migrate PG database hosted on Heroku -Rails

Im getting this error when running heroku run rake db:migrate -> ERROR: column "frequency" cannot be cast automatically to type integer I'm not sure what I'm supposed to do in order to fix this error. Here is my schema local that works fine when I migrated:
schema:
create_table "assignments", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "attachment"
t.integer "finished_count"
t.datetime "start_at"
t.datetime "end_at"
t.integer "frequency"
t.integer "status", default: 0
t.index ["status"], name: "index_assignments_on_status"
end
Cannot I not use integer as a type with PG? if not, what should I do instead?
Without seeing your migration, which you should post. A likely cause is that the frequency column already has data in it. Therefore PG cannot blindly convert that data into a integer if it is not something that is like a number.

Unable to delete tables in schema.rb from rails app

I use Rails 4.0 and Ruby 2.3.0
I have a small Rails App. At first I wanted to assign different roles to each user based what he/she had signed up for. So i tried out the "Royce" gem from https://github.com/MartinJNash/Royce
I uninstalled it after seeing that it was too complicated for my requirements.
Then I tried the "simple-roles" gem to see how it works from here https://github.com/stanislaw/simple_roles. I uninstalled tat too. I ran db:migrate when I had these 2 gems in my app.
Now I don't want the corresponding tables from these two gems in my schema.rb. Its not exactly interfering with my app, but I would like to delete them.
The following is my schema.rb file
ActiveRecord::Schema.define(version: 20160911213902) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "roles", force: :cascade do |t|
t.string "name"
t.integer "resource_id"
t.string "resource_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree
add_index "roles", ["name"], name: "index_roles_on_name", using: :btree
create_table "royce_connector", force: :cascade do |t|
t.integer "roleable_id", null: false
t.string "roleable_type", null: false
t.integer "role_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "royce_connector", ["role_id"], name: "index_royce_connector_on_role_id", using: :btree
add_index "royce_connector", ["roleable_id", "roleable_type"], name: "index_royce_connector_on_roleable_id_and_roleable_type", using: :btree
create_table "royce_role", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "royce_role", ["name"], name: "index_royce_role_on_name", using: :btree
create_table "sessions", force: :cascade do |t|
t.string "session_id", null: false
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "sessions", ["session_id"], name: "index_sessions_on_session_id", unique: true, using: :btree
add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at", using: :btree
create_table "users", force: :cascade do |t|
t.string "email"
t.string "name"
t.string "login_token"
t.datetime "login_token_valid_until"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users_roles", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "role_id"
end
add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id", using: :btree
create_table "vendors", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "visits", force: :cascade do |t|
t.string "country"
t.datetime "visited_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Even though i have un-installed the 2 roles-gems, everytime I run rake db:migrate, I get the above schema.
I am using only the "sessions" and "vendors" tables in my app.
Any idea what I should do to get rid of these unwanted tables?
Firstly check if the table is present...in rails console...
##to view all tables
ActiveRecord::Base.connection.tables
i use rails console to delete tables directly...
ActiveRecord::Base.connection.execute("drop table table_name")
###========OR===============
ActiveRecord::Migration.drop_table(:table_name)
###=========OR===============
ActiveRecord::Migration.drop_table("table_name")
###=========WITH EXAMPLE===============
ActiveRecord::Base.connection.drop_table :subscriptions
Then,delete the migration file or rename it again by changing timestamp and again run rake db:migrate
Hope it helps :)
Look in your app/db/migrate directory. Find and delete the migrations associated with those gems.
Once the migration files are removed, you can run rake db:migrate again. Then run rake db:schema:dump and check the newly generated schema.rb file to make sure you're not retaining any tables for the gems you removed.

Remove Tables from Schema

This is probably a fairly basic question, but I'm at a loss on how to clean up after installing a gem I decided not to use. During the install process of Attachinary, the install instructions said to run rake attachinary:install:migrations - creating a new table and index in my schema, noted here:
create_table "attachinary_files", force: :cascade do |t|
t.integer "attachinariable_id"
t.string "attachinariable_type"
t.string "scope"
t.string "public_id"
t.string "version"
t.integer "width"
t.integer "height"
t.string "format"
t.string "resource_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "attachinary_files", ["attachinariable_type", "attachinariable_id", "scope"], name: "by_scoped_parent", using: :btree
I later decided to go with a simplier attachement gem and I'm attempting to clean up and remove all the "stuff" created during the attachinary install.
Any advice on how to clean up the db? I'm running Postgresql if that makes any difference.
Create a migration:
bundle exec rails g migration remove_attachinary
Then tell Rails what to do:
def up
drop_table :attachinary_files
end
Remove the change method present by default
If ever you'd like this migration to be reversible, copy your previous code in a down method

new record in rails console error

A very similar question was already asked, bud I can't solve the problem anyway. I am trying to create a new record in rails console and I get this error:
2.1.2 :001 > subject = Subject.new
Mysql2::Error: Table 'simple_cms_development.subjects' doesn't exist: SHOW FULL FIELDS FROM `subjects`
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'simple_cms_development.subjects' doesn't exist: SHOW FULL FIELDS FROM `subjects`
Can somebody please very specifically tell my what should I do?
Here's subject.rb:
class Subject < ActiveRecord::Base
end
and schema.rb:
ActiveRecord::Schema.define(version: 20140617074943) do
create_table "admin_users", force: true do |t|
t.string "first_name", limit: 25
t.string "last_name", limit: 50
t.string "email", default: "", null: false
t.string "username", limit: 25
t.string "password", limit: 40
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "pages", force: true do |t|
t.integer "subject_id"
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible", default: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "pages", ["permalink"], name: "index_pages_on_permalink", using: :btree
add_index "pages", ["subject_id"], name: "index_pages_on_subject_id", using: :btree
create_table "sections", force: true do |t|
t.integer "page_id"
t.string "name"
t.integer "position"
t.boolean "visible", default: false
t.string "content_tipe"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "sections", ["page_id"], name: "index_sections_on_page_id", using: :btree
end
create_subjects.rb:
class CreateSubjects < ActiveRecord::Migration
def up
create_table :subjects do |t|
t.string "name"
t.integer "position"
t.boolean "visible" :default => false
t.timestamps
end
end
def down
drop_table :subjects
end
end
Add a comma in
t.boolean "visible" :default => false`
as in
t.boolean "visible", :default => false`
and then run rake db:migrate
Making sure that config/database.yml file has a valid entry for a database connection on your machine. Look at the development stanza.
More on migrations at guides.rubyonrails.org/migrations.html
More on configuring a database and the database.yml file at
http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
You need to create a subjects table that defines the attributes you want to persist in the Subject instances.
So say you want title and description. Use this command to create the migration:
rails generate migration subjects title:string description:text
And then run the command
rake db:migrate
Then try your Subject.new command
Alternatively, if you do not want to persist any subject attributes, change the subject class definition to:
class Subject
end

Resources