I am doing an online course , and I am trying to design a website with a user database . I have built a " Contact Us" page with a form/table .
I cannot make the timestamps show in the rails console $ Contact.all
this is my Schema.rb :
ActiveRecord::Schema.define(version: 20190518115940) do
create_table "contacts", force: :cascade do |t|
t.string "name"
t.string "email"
t.text "comments"
end
end
And this is the crate_contacts.rb file :
class CreateContacts < ActiveRecord::Migration[5.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
Your schema.rb knows nothing about timestamps. Did you created the table without timestamps and then you added it to the migration?
If so, you can rerun the migration (assuming it is the last one) with the command rails db:migrate:redo, but all the data in that table will be wiped.
Refer to this guide for further options.
Related
I'm trying to rename a column that is definitely there in the schema and all the info is there, but when I go to update it it's giving me a "missing column" error and I can't figure out why? Any ideas?
Migration:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :postcodes, :type, :zip_type
end
end
schema.rb
create_table "postcodes", force: true do |t|
t.string "postalcode"
t.string "type"
t.string "primary_city"
t.string "state"
t.string "county"
t.string "timezone"
t.string "area_code"
t.string "latitude"
t.string "longitude"
t.string "estimated_population"
t.datetime "created_at"
t.datetime "updated_at"
end
Error:
DL is deprecated, please use Fiddle
DL is deprecated, please use Fiddle
== 20150304172437 ChangeColumnName: migrating =================================
-- rename_column(:postcodes, :type, :zip_type)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Missing column postcodes.typeC:/Users/Steve Q/Documents/GitHub/project1/db/migrate/20150304172437_change_column_name.rb:3:in `change'C:in `migrate' ActiveRecord::ActiveRecordError: Missing column postcodes.type
I have tried renaming type column to zip_type its working. Another solution is you can delete type column and add new column as zip_type.
Here are my two models
class Comment < ActiveRecord::Base
belongs_to :phone
end
class Phone < ActiveRecord::Base
has_many :comments, :dependent => :destroy
end
Here is the schema for the tables
ActiveRecord::Schema.define(version: 20131119231249) do
create_table "comments", force: true do |t|
t.string "username"
t.string "ipaddy"
t.text "pcomments"
t.string "company"
t.string "calltype"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "pnumber"
t.string "source"
end
create_table "phones", force: true do |t|
t.integer "pnumber"
t.text "mrcomment"
t.integer "ccount"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Here is the raw SQL that works
SELECT phones.ccount ,
comments.*
FROM phones
INNER JOIN comments
ON phones.pnumber = comments.pnumber;
When I run the following in my controller
#phones = Phone.select("phones.ccount, comments.*").joins(:comments).where(:comments => {comments.pnumber => phones.pnumber})
I get the following error
undefined local variable or method `comments' for #<FrontPageController:0x00000003c56c70>
Any help on what the active record statement should like would be greatly appreciated
It seems like you're using the select() erroneously. Have you read the docs: http://apidock.com/rails/ActiveRecord/QueryMethods/select ?
from docs: "Second: Modifies the SELECT statement for the query so that only certain fields are retrieved:"
The query's syntax should more look like (using a standard example):
l = Location.where(["id = ?", id]).select("name, website, city").first.
Is there a way to specify a database level uniqueness constraint on a field in a Rails 3.1+ migration? I know ActiveRecord's validates_uniqueness_of would work, but I'm curious to know if this can be specified elsewhere.
For example, in order to specify that the 'login' field should be unique in a migration such as ...
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :login
t.timestamps
end
end
end
... I'd like to add an option like :unique => true.
There's nothing mentioned in the Documentation so I assume that's not a great place/way to do things, but does anyone know how this might be achieved (even if it's manually added to the resulting schema)? Or is validates_uniqueness_of the preferred way to go?
You can add unique index so:
def change
create_table :users do |t|
t.string :name
t.string :login
t.timestamps
end
add_index :users, :login, :unique => true
end
when i deal with a migrate data,i want to make each record unique in the table the migrate file make.here is my migrate file:
def self.up
create_table :rbac_mandata do |t|
t.integer :mandator_id
t.integer :mandatarius_id
t.integer :permission_id
t.timestamps
end
add_index :rbac_mandata, [:mandator_id, :mandatarius_id, :permission_id], :unique => true
end
when i execute rake task and find it not work.but when i change it to this,it was done:
def self.up
create_table :rbac_mandata do |t|
t.integer :mandator_id
t.integer :mandatarius_id
t.integer :permission_id
t.timestamps
end
add_index :rbac_mandata, [:mandator_id, :permission_id], :unique => true
end
The generated index name is probably too long, it's probably over 64 characters. Just define the name manually and it'll go through fine, like this...
add_index :rbac_mandata, [:mandator_id, :mandatarius_id, :permission_id], :unique => true, :name => 'my_index_name_here'
in my rails3 app i have a debitnote controller ,model and views created by scaffold.
i show you the migration:
class CreateDebitnotes < ActiveRecord::Migration
def self.up
create_table :debitnotes do |t|
t.string :debit_note_number
t.date :date
t.decimal :amount, :precision =>18, :scale => 2
t.string :debit_account_of
t.string :debit_for
t.text :description
t.timestamps
end
end
def self.down
drop_table :debitnotes
end
end
in my app i want to automatically generate a unique debitnote number when user open new form to create a deb
You may use UUID generator github repo