Sort columns active scaffold - ruby-on-rails

I am trying to list/order my columns in a custom order with active scaffold, Rails 5.1.2 and Ruby 2.3.3p222.
Migration as follows:
class CreateResources < ActiveRecord::Migration[5.1]
def change
create_table :resources do |t|
t.string :firstname
t.string :surname
t.date :date_of_birth
t.string :identity_number
t.string :nationality
t.string :state_province
t.date :interview_date
t.string :available
t.string :home_phone
t.string :mobile_phone
t.timestamps
end
end
end
And I have added columns, the migration is as follows:
class AddColumnsToResources < ActiveRecord::Migration[5.1]
def change
add_column :resources, :expected_salary, :string
add_column :resources, :current_ctc, :string
add_column :resources, :years_of_experience, :string
add_column :resources, :notice_period, :string
end
end
I then used paperclip to add an attachment to the table:
class AddAttachmentAttachmentToResources < ActiveRecord::Migration[5.1]
def self.up
change_table :resources do |t|
t.attachment :attachment
end
end
def self.down
remove_attachment :resources, :attachment
end
end
I need to sort my columns so they appear in the following order:
firstame
surname
identity number
...

Related

Group has_and_belongs_to_many

Given a model with ActiveStorage: DayFrame, Day, Session. Models and migration see below. DayFrame and Session are many-to-many relationship. day_frames_sessions intermediate join table that includes foreign keys referring to each of the classes DayFrame and Session. How can I get day_frames group by session and sort by day.date like this:
{session_id_1=> [DayFrame1, DayFram2, DayFram3], session_id_2 => [DayFrame2, DayFram1, DayFram4]}
class DayFrame
has_and_belongs_to_many :sessions
belongs_to :day
end
class Day
has_and_belongs_to_many :sessions
has_many :day_frames
end
class Session
has_and_belongs_to_many :conference_day_frames
end
The migrations are:
class CreateDay < ActiveRecord::Migration[5.2]
def up
create_table :days do |t|
t.date :date
t.string :day_type
t.timestamps
end
end
def down
drop_table :days
end
end
class CreateDayFrame < ActiveRecord::Migration[5.2]
def up
create_table :day_frames do |t|
t.string :name
t.time :begin_time
t.time :end_time
t.string :frame_type
t.timestamps
t.references :day, index: true
end
def down
drop_table :day_frames
end
end
class CreateSessions < ActiveRecord::Migration[5.2]
def up
create_table :sessions do |t|
t.integer :number, limit: 1
t.string :title
t.string :short_title
t.boolean :is_active
t.timestamps
end
end
def down
drop_table :sessions
end
end
class CreateDayFramesSessions < ActiveRecord::Migration[5.2]
def up
create_join_table :day_frames, :sessions do |t|
t.index :day_frame_id
t.index :session_id
end
end
def down
drop_table :day_frames_sessions
end
end

"TypeError: no implicit conversion of nil into String" on HABTM association

I have to deal with this error when I try to associate a record to another one via a HABTM association:
Person.first.communities = Communities.all
Models and migrations:
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.string :name
t.string :email
t.timestamps null: false
end
end
end
class CreateCommunities < ActiveRecord::Migration
def change
create_table :communities do |t|
t.string :name
t.text :description
t.timestamps null: false
end
end
end
class CreateJoinTablePersonCommunity < ActiveRecord::Migration
def change
create_join_table :people, :communities do |t|
# t.index [:person_id, :community_id]
# t.index [:community_id, :person_id]
end
end
end
I use the pg (0.18.4) gem with the Postgres (9.5.2)
Youcan use below code to create relationship.
Person.first.communities << Communities.all
If this not works please check your associations via reflect association method on the model.

How to inherit from another Rails migration?

I have a Rails migration for a simple User model:
class Users < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name, :default => :null
t.float :weight
t.datetime :recorded_at
t.timestamps
end
end
end
I would like to have a second table for the history of the user. It should have the same columns but another name, obviously. Also it should reference the user table.
require_relative '20130718143019_create_history.rb'
class History < Users
def change
create_table :history do |t|
t.references :user
# ...?
end
end
end
How can use inheritence to avoid copying all the migration configuration?
After leaving the keyboard tomatoes fell off my eyes and it was clear how I can set this up:
class Users < ActiveRecord::Migration
def change
create_table :users do |t|
prepare_columns(t)
end
end
protected
def prepare_columns(t)
t.string :name, :default => :null
t.float :weight
t.datetime :recorded_at
t.timestamps
end
end
...
require_relative '20130718143019_create_history.rb'
class History < Users
def change
create_table :history do |t|
t.references :user
prepare_columns(t)
end
end
end

Rails 3.1 add columns with change_table migration

I have one table called profiles with some columns.
Now I wish to add a few columns to this table using the change-method in rails 3.1. I created a migration with the following code:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
The migration works perfectly, but when I want to rollback I get
SQLite3::SQLException: duplicate column name: photo: ALTER TABLE "profiles" ADD "photo" varchar(255)
Any ideas why?
The auto-generated migrations for adding columns in Rails 3.1 is in the format:
class AddColumnToTable < ActiveRecord::Migration
def change
add_column :table, :column, :type
end
end
Perhaps try that syntax?
It looks like you need to tell the migration how to revert itself:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
reversible do |dir|
dir.down do
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
end
end
See http://guides.rubyonrails.org/migrations.html#using-reversible for more information.
Alternatively you could try using the old up and down methods which are still available like so:
def up
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
def down
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
More on up/down here: http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods

Ruby On Rails Migration

I have seen two different ways of migrating a database. Which one is the proper way to do it in Rails 3?
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.timestamps
end
end
and
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :name, :string
t.timestamps
end
end
Thank You!
t.string :title is just a shortcut for t.column :title, :string
Both of them are ok, there is no discrimination. I'd normally prefer the short form, as it is more readable to me but it's just a matter of opinion.

Resources