ruby - Generate Rows And Columns from Rails Console -
i have small predicament.
i have table called users. table users has relationship table called user notification. here migration user table:
create_table "user_notifications", force: :cascade |t| t.belongs_to :user t.integer "other_user" t.integer "unseen_count", default: 0 t.datetime "created_at", null: false t.datetime "updated_at", null: false end
the problem when create new user table populated row of new user. how add row existing users. needed function of app. using putty connect server tool have rails console.
example: have 60 users. how have 60 users row in new table using rails console.
edit: method use whenever user signed up. can use method within rails console or create deviation of this?
user.rb:
after_create :create_user_notification def create_user_notification usernotification.create(:user_id => self.id) end
thank in advance.
if understand correctly want add notification each user record.
given have models this:
class user has_many :user_notifications end class usernotification belongs_to :user end
to insert record each user this:
user.all.find_in_batches(batch_size: 10).each |u| u.user_notifications.create(message: 'hello dear') end
.find_in_batches
might not matter if have 60 records if have 60,000 server run out of memory.
Comments
Post a Comment