Answer the question
In order to leave comments, you need to log in
How to implement such structure in rails migration?
How to implement such structure in rails migration?
1. Users and News
table 2. Set Users table to primary key Login with String type
3. Associate Users with News as one-to-many on Login field with String type
Answer the question
In order to leave comments, you need to log in
Try something like this.
class News < ActiveRecord::Base
belongs_to :user, foreign_key: :login, primary_key: :login
end
class User < ActiveRecord::Base
has_many :news, foreign_key: :login, primary_key: :login
end
class CreateNews < ActiveRecord::Migration
def up
create_table :news do |t|
t.string :login, limit: 50
t.timestamps null: false
end
add_foreign_key :news, :users, column: :login
end
def down
remove_foreign_key :news, name: :login
drop_table :news
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users, id: false, primary_key: :login do |t|
t.string :login, limit: 50, null: false
t.timestamps null: false
end
end
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question