rails unable to index a foreign key - ruby-on-rails

I have a two separate migration files:
class CreateLeadProfiles < ActiveRecord::Migration
def change
create_table :lead_profiles do |t|
t.belongs_to :Contact
t.timestamps
end
add_index :lead_profiles, :contact_id
end
end
and:
class CreateClientProfiles < ActiveRecord::Migration
def change
create_table :client_profiles do |t|
t.belongs_to :Contact
t.belongs_to :LeadProfile
t.timestamps
end
add_index :client_profiles, :contact_id
add_index :client_profiles, :lead_profile_id
end
end
when running rake db:migrate, I get the following error:
Mysql2::Error: Key column 'lead_profile_id' doesn't exist in table: CREATE INDEX `index_client_profiles_on_lead_profile_id` ON `client_profiles` (`lead_profile_id`)
client_profile belongs_to lead_profile, and so should have a lead_profile_id. And I want that foreign key to be indexed. What am I doing wrong?

Try:
class CreateClientProfiles < ActiveRecord::Migration
def change
create_table :client_profiles do |t|
t.belongs_to :contact
t.belongs_to :lead_profile
t.timestamps
end
add_index :client_profiles, :contact_id
add_index :client_profiles, :lead_profile_id
end
end

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.

PG::Error: ERROR: column tutorials.tutorialcategory_id does not exist

I have two models 'Tutorial' and 'Tutorialcategory'
class Tutorialcategory < ActiveRecord::Base
has_many :tutorials
class Tutorial < ActiveRecord::Base
belongs_to :tutorialcategory
Tutorials are associated with multiple categories like html,rubyonrails where html and ruby on rails are tutorialcategories
followings are migrations
class CreateTutorials < ActiveRecord::Migration
def change
create_table :tutorials,force: true do |t|
t.string :title
t.text :body
t.integer :rating
t.string :videoid
t.belongs_to :tutorialcategory
t.timestamps
end
end
end
class CreateTutorialcategories < ActiveRecord::Migration
def change
create_table :tutorialcategories do |t|
t.string :title
t.timestamps null:false
end
end
end
All tutorials are listing properly on index page but when I see category page it gives me following error
PG::Error: ERROR: column tutorials.tutorialcategory_id does not exist
I have no idea why you named your model Tutorialcategory instead of TutorialCategory which follow Rails naming convention and make it easier to understand.
Firstly, rollback your db one step
rake db:rollback
Change your migration file to:
class CreateTutorials < ActiveRecord::Migration
def change
create_table :tutorials,force: true do |t|
t.string :title
t.text :body
t.integer :rating
t.string :videoid
t.belongs_to :tutorial_category, index: true
t.timestamps
end
end
end
class CreateTutorialCategories < ActiveRecord::Migration
def change
create_table :tutorial_categories do |t|
t.string :title
t.timestamps null:false
end
end
end
Run the migration again and edit your model to match with new schema.
class TutorialCategory < ActiveRecord::Base
has_many :tutorials
class Tutorial < ActiveRecord::Base
belongs_to :tutorial_category

How to add database level foreign key in the migration file in rails 4.2?

I have gone through some of similar question people asked but couldn't find the appropriate solution for it. I have also seen some people using this method - add_foreign_key
class CreateTaskLists < ActiveRecord::Migration
def change
create_table :task_lists do |t|
t.string :name
t.references :user
t.timestamps
end
add_foreign_key :task_lists, :users
end
end
but it is throwing undefined method error.
undefined method `add_foreign_key' for
#<CreateTaskLists:0x007ffe9a5cd578>
/Users/sushilkumar/.rvm/gems/ruby-2.2.3/gems/
activerecord-4.0.0/lib/active_record/
migration.rb:624:in `block in method_missing'
How to add foreign key in rails migration with different table name
I don't know, How does this work for them?
You can simply try this way using references
class CreateTaskLists < ActiveRecord::Migration
def change
create_table :task_lists do |t|
t.string :name
t.references :user, index: true
t.timestamps
end
add_foreign_key :task_lists, :users
end
end
class CreateTaskLists < ActiveRecord::Migration
def change
create_table :task_lists do |t|
t.string :name
t.references :user, index: true
t.timestamps
end
add_foreign_key :task_lists, :users
end
end
try this
Did you meant to reference the table User and not Users? If so, you must use the singular (:user) when making a reference:
class CreateTaskLists < ActiveRecord::Migration
def change
create_table :task_lists do |t|
t.string :name
t.references :user
t.timestamps
end
add_foreign_key :task_lists, :users
end
end
Hope this will work for you.
class CreateTaskLists < ActiveRecord::Migration
def change
create_table :task_lists do |t|
t.string :name
t.references :user
t.timestamps
end
add_foreign_key :users, :task_lists
end
end

Rails add_index missing when running GENERATE command

Since I am new to Rails, I am following this 'getting started' guide on rails website.
On section 6.1 Generating a Model, I should run rails generate model Comment commenter:string body:text post:references to get a comment model.
Supposedly, this is what should be included in the migration file:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
But in my migration file, I have everything but this line add_index :comments, :post_id.
Instead, I have index:true following the t.references :post
I can't seem to find an explanation to this, can anyone explain to me what is going on here? Because later on I need to use :post_id, but in my version of migration, it is not clearly declared. I am very confused.
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
and
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post, index: true
t.timestamps
end
end
end
will do same things, both of them will add index to post_id column
also you can later add index to any column any column that you want to add to your model like
$ rails generate migration AddPartNumberToProducts part_number:string:index
that will produce this code:
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_index :products, :part_number
end
end
in above code if you want to create index for post_id then write it as t.integer :post_id
else you can try this add_index :comments, :post

Resources