I have to tables that have a many to many relationship. I have created the correct table codesecure_project_tst_definition and it works. I can join rows together by calling the codesecure_projects << method on a TstDefinition object. The problem is that for some reason active record wants to use Codesecure_project_id as the id value for the codesecure_project_tst_definition table. What am I doing wrong? How do I fix it so that when I call the codesecure_projects << method it does not try to set the id of the codesecure_project_tst_definition table?
I have posted the migrations below
class CreateCodesecureProjects < ActiveRecord::Migration
def self.up
create_table :codesecure_projects do |t|
t.string :name
t.string :lang
t.timestamps
end
end
def self.down
drop_table :codesecure_projects
end
end
class CreateTstDefinitions < ActiveRecord::Migration
def self.up
create_table :tst_definitions do |t|
t.string :test_name
t.timestamps
end
end
def self.down
drop_table :tst_definitions
end
end
class CreateCodesecureProjectsTstDefinitions < ActiveRecord::Migration
def self.up
create_table :codesecure_projects_tst_definitions do |t|
t.references :codesecure_project
t.references :tst_definition
t.timestamps
end
end
def self.down
drop_table :codesecure_projects_tst_definitions
end
end
The relevant parts of the models:
class TstDefinition < ActiveRecord::Base
has_and_belongs_to_many :codesecure_projects
has_many :tst_datas
class CodesecureProject < ActiveRecord::Base
has_many :input_abstractions
has_and_belongs_to_many :tst_definitions
After some searching I actually found the answer, thanks to this blog post http://jimcortez.com/blog/?p=9. I simply needed to remove the id column from the codesecure_projects_tst_definitions table. So the migration now looks like this:
class CreateCodesecureProjectsTstDefinitions < ActiveRecord::Migration
def self.up
create_table :codesecure_projects_tst_definitions, :id => false do |t|
t.references :codesecure_project
t.references :tst_definition
t.timestamps
end
end
def self.down
drop_table :codesecure_projects_tst_definitions
end
end
Related
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
I'm trying to create a situation in which I have the ability to create a series of studies. So I have a table called Series. Each Series will have a name. Each Series also has_many :parts. So I have a table called Part and it belongs_to :series. Then each Part has_many :questions and my Question table belongs_to :part. I have to attribute each of these tables to each other so I have the migrations setup like this:
class CreateSeries < ActiveRecord::Migration
def self.up
create_table :series do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :series
end
end
class CreateParts < ActiveRecord::Migration
def self.up
create_table :parts do |t|
t.date :date
t.string :book
t.integer :series_id
t.timestamps
end
add_index :parts, [:series_id, :created_at]
end
def self.down
drop_table :parts
end
end
class CreateQuestions < ActiveRecord::Migration
def self.up
create_table :questions do |t|
t.text :body
t.integer :part_id
t.timestamps
end
add_index :questions, [:part_id, :created_at]
end
def self.down
drop_table :questions
end
end
So I migrated the database and using SQLite DB Browser, my Questions table has a parts_id which it should. And my Parts table should have a series_id column, but it does NOT. Could someone please help with this issue? Thanks.
I wonder why the series column is not in your database. ok to perform a safe check if i may call it try running the model generator again. This time run it like this
rails g model part date:date book series:belongs_to and see if you get the same issue. I hope this helps. Or you can also check with your console window try creating a new part with a random series id and check if it has pluralization issues.
Open the application console rails c and type Part.
You should see the attribute series_id.
If you do, the solution is simply restarting the server.
I'm having this table
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.integer :subcategory
t.string :event_name
t.text :description
t.string :location
t.date :date
t.decimal :price
t.timestamps
end
end
def self.down
drop_table :events
end
end
and i want to change the subcategory to subcategory_id. I tries this one but is not working
ruby script/generate migration RenameDatabaseColumn and then i went to the file which is in db\migrate and edited to look like this
class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subgategory, :subgategory_id
end
def self.down
# rename back if you need or do something else or do nothing
end
end
then i run the command rake db:migrate put the column is still subcategory. Can you help me please? I'm using rails 2.0
Thank you
Did you misspell the column name? isn't it :subcategory? You wrote :subgategory.
class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subcategory, :subcategory_id
end
def self.down
# rename back if you need or do something else or do nothing
end
end
I'm develop a web app with Rails 3.0.9 and Postgres 9.4
I'm trying to create a join table for a has_and_belongs_to_many association, but when execute "rake db:migrate" the only one not executed migration is the migration for join table.
Rails didn't show any error, only didn't create the table.
When I do the rollback, rails show a error because couldn't drop the table because don't exist.
Here is the code of migration:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def self.up
def change
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
end
def self.down
drop_table :campanas_locals
end
end
Anyone have an idea? Thanks!
Rails 3.0.X try:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def self.up
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
def self.down
drop_table :campanas_locals
end
end
Rails 3.1.X try:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def change
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
end
I'm diving into RoR and I need to remove a model and its table, as well as update the other models that reference it. I did a search on google and SO and the best answer I found was this, but the answer is unclear to me. The final consensus was to use the ruby script/destroy model method and then "manually edit any migrations that might contain refs to these deleted models" It's this last part that I'm unclear about. I want to delete the models for my user and profile models and tables...
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
def self.down
drop_table :users
end
end
class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|
t.string :name
t.integer :user_id
t.timestamps
end
end
def self.down
drop_table :profiles
end
end
and update the article model and table that references them...
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.datetime :published_at
t.string :image
t.timestamps
end
end
def self.down
drop_table :articles
end
end
class AddUserIdToArticles < ActiveRecord::Migration
def self.up
add_column :articles, :user_id, :integer
end
def self.down
remove_column :articles, :user_id
end
end
Can I just do ruby script/destroy user and then call the self.down methods in the article migrations? If so, how do I call the 'self.down` methods and in what order?
Thanks so much in advance for your help!
Yep. Just delete it with
ruby script/destroy model user
ruby script/destroy model profile
And then rollback your database, or self.down with this :
rake db:rollback
Now you can safely delete your migration file.