rails-migrations

RnR: Database normalization, rails models and associations

末鹿安然 提交于 2020-01-06 12:23:41
问题 Ok, so I have three different objects: a person, place, and equipment. Each can have an address, in most cases multiple address and multiple phone numbers. So my thought is create the specific object tables, the have an address and a phone table. Here my question: So typically, in my SQL world, I would just have an object_id column in the address and phone table and put a id of the object in the object id column, and then select all the address or phone records that match. I could do this, do

failing migrations : PG::ProtocolViolation Rails

本秂侑毒 提交于 2020-01-05 07:29:09
问题 I just updated to rails4 and realized that my db roles and database was somehow not setup. So I dropped the db and re-created the app_development db and the roles. Now I am trying to run rake db:migrate and its failing with following error: == DeviseCreateUsers: migrating ============================================== -- create_table(:users) -> 0.0081s -- add_index(:users, :email, {:unique=>true}) -> 0.0031s -- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0030s ==

Rails 3 - DB seed data validation

为君一笑 提交于 2020-01-02 09:55:56
问题 I am seeding a test database in Rails 3.1 through thousands of create calls in the seeds.rb file. A little problem arises when these calls do not pass the model validations: rails will not notify me this, and the seeding goes on correctly until the end of the file. At the end of the process I do not know which records have been created and which aren't, unless I check them one by one ... Is there a way to get notified when records do not pass validations when using rake db:seed or rake db

add a database column with Rails migration and populate it based on another column

半世苍凉 提交于 2020-01-01 09:22:11
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(

add a database column with Rails migration and populate it based on another column

浪子不回头ぞ 提交于 2020-01-01 09:22:09
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(

heroku run rake db:migrate error

岁酱吖の 提交于 2019-12-31 07:17:09
问题 I want do run migration on my app that I have on heroku but I get this error: Running `rake db:migrate` attached to terminal... up, run.1 DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has

Why am I asked to run 'rake db:migrate RAILS_ENV=test'?

爱⌒轻易说出口 提交于 2019-12-29 02:33:09
问题 On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec : /Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in `check_pending!': Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError) That doesn't seem right. No one migrates their test database, do they? They db:test:prepare it, which—to be

Generate an auto increment field in rails

自闭症网瘾萝莉.ら 提交于 2019-12-28 10:07:27
问题 I have a model Token, which has a field token_number that I need to auto increment (starting from 1001), if and only if the user does not provide it. The problem is that, since the user has the option to provide this field, I can't exactly query the database and ask for the largest token_number. I found one answer on this forum, but I'm quite certain there has to be a better way to do it than to execute an SQL statement? Auto increment a non-primary key field in Ruby on Rails 回答1: Interesting

Rails: How to migrate a database where I added a :belongs_to relationship?

北城以北 提交于 2019-12-25 03:56:12
问题 This is my first rails app, one I'm creating for the sole purpose of learning rails. I created an app where I have Users and Products (and sessions, but this is not relevant here). After doing a rake db:mograte creating a few items and testing, I then wanted to add a relationship, where products belongs_to :users and a users has_many :products. But of course, since I have already created the tables, there isn't any column there to save this information. How do I make a migration on the

add has_many and belongs_to migration from command line

不打扰是莪最后的温柔 提交于 2019-12-25 02:23:01
问题 I generated two models and now want to implement active record associations. I have Designers and Items. An Item belongs to a Designer and a Designer has many Items. My models look like this: app/models/item.rb: class Item < ActiveRecord::Base belongs_to :designer validates :designer_id, presence: true end app/models/designer.rb: class Designer < ActiveRecord::Base has_many :items, dependent: :destroy end Even after I run rake db:migrate my migrations don't reflect the new relationship. They