rails-migrations

Cannot generate any migrations for Rails 4.2, Ruby 2.1.4. Syntax error in bin/rails

三世轮回 提交于 2019-12-25 01:09:03
问题 Whenever I try to create a migration, I get the following error. Does anyone know what could be causing this? $ rails generate migration NewMigration /Users/myuser/.rvm/gems/ruby-2.1.4/bin/ruby_executable_hooks:15:in `eval': /Users/myuser/.rvm/gems/ruby-2.1.4/bin/rails:19: syntax error, unexpected '=', expecting keyword_end (SyntaxError) /Users/myuser/.rvm/gems/ruby 2.1.4/bin/rails:23: syntax error, unexpected end-of-input, expecting keyword_end from /Users/myuser/.rvm/gems/ruby-2.1.4/bin

Rails3 Mysql2::Error: Unknown column - ActiveRecord::StatementInvalid

倖福魔咒の 提交于 2019-12-23 18:09:44
问题 I'm new to working with databases in Rails at this level, and I've looked for several hours but haven't found a solution to this specific problem. Versions: Rails 3.2.9, Ruby 1.9.3, MySQL 5.5.28 (mysql2 gem 2.9.0), Mac OS 10.7.5 Error: ActiveRecord::StatementInvalid in Action_figures#list Mysql2::Error: Unknown column 'action_figures.position' in 'order clause': SELECT `action_figures`.* FROM `action_figures` ORDER BY action_figures.position ASC Extracted source (around line #14): [list.html

Foreign key in rails 4

独自空忆成欢 提交于 2019-12-23 09:36:38
问题 I'm using Rails 4 and SQLite. I'm trying to add foreing keys to my indicators table. Please see my code below class Indicator < ActiveRecord::Base belongs_to :objetive belongs_to :responsible, class_name: "Person" end The migration script: class AddFksToIndicator < ActiveRecord::Migration def change add_reference :indicators, :objective, index: true add_reference :indicators, :responsible, index: true end end when run the migration all is ok, so I tried in console: 2.0.0p247 :002 > i =

association and migration between users and teams (rails)

只愿长相守 提交于 2019-12-22 11:30:31
问题 I have this User and team model which has the following association: user.rb class User < ActiveRecord::Base belongs_to :team team.rb class Team < ActiveRecord::Base has_many :users has_one :leader, class_name: "User", foreign_key: "leader_id" belongs_to :manager, class_name: "User", foreign_key: "manager_id" but it seems that I can't imagine representing it properly into a migration. At first, this is what I did: class AddTeamIdToUsers < ActiveRecord::Migration def change add_column :users,

Rails + Postgres migration - why am I receiving the error “PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist”?

左心房为你撑大大i 提交于 2019-12-22 03:43:46
问题 One of my Rails migrations uses a uuid as the primary key. The Postgres extension gen_random_uuid() should solve this issue, but I continue to get the error after installing the relevant extension ( uuid-ossp ). 回答1: The issue was that the uuid-ossp extension was being blown away with the database each time I dropped the db as part of a reset and migration (e.g. rake db:drop db:create db:migrate ). The fix is to create a migration that's run before all other migrations which enables the

Rails Models relationships

别等时光非礼了梦想. 提交于 2019-12-22 01:16:25
问题 Hello fellow developers! Recently I've been playing with Rails 3.0 and after quite a bit of research I'm kinda stuck. I want to know what approach or solution is the best in my case(I couldn't find an answer yet). So what I'm trying to achieve is simple and straight forward. I want to do something like this: class User < ActiveRecord::Base has_many :feeds has_many :casts, :through => :feeds end class Feed < ActiveRecord::Base has_many :users has_many :casts end class Cast < ActiveRecord::Base

What determines if rails includes id: :serial in a table definition?

可紊 提交于 2019-12-21 07:48:08
问题 I'm working with an existing rails app, using postgresql. Its schema.rb file has id: :serial for many, but not all, tables: create_table "foos", id: :serial, force: :cascade do |t| When I run rails db:migrate:reset , id: :serial is removed. We are all on the same version of postgres, but different OSes. I haven't exhaustively tested the behavior between machines, but I think there is a difference between machines. The rails version is the same as it was when the project started. The project

How to reset auto increment field in a ActiveRecord migration?

人盡茶涼 提交于 2019-12-20 17:47:30
问题 In my migration I have: def up MyModel.destroy_all MyModel.create!({:id=>1,:name=>'foo'}) MyModel.create!({:id=>2,:name=>'fooBar'}) MyModel.create!({:id=>3,:name=>'fooNull'}) end because I need to override data that was already on my_models table But Even though I'm specifying the id on MySQL it continues the numbering from the position it already was. I need to rest the counter on the auto increment for id to have only this 3 new records with that id values trough Active Record migration on

How to create a migration to remove an index only if it exists, rather than throwing an exception if it doesn't?

喜欢而已 提交于 2019-12-20 16:17:32
问题 Right now, the current migration might fail, if the books table doesn't have created_at or updated_at fields: class AddTimestampIndexes < ActiveRecord::Migration def up remove_index :books, :created_at remove_index :books, :updated_at add_index :books, :created_at add_index :books, :updated_at end def down remove_index :books, :created_at remove_index :books, :updated_at end end Does remove_index take any options to silently proceed if it fails to remove the index rather than raising an error

Best SQL indexes for join table

血红的双手。 提交于 2019-12-20 10:29:31
问题 With performance improvements in mind, I was wondering if and which indexes are helpful on a join table (specifically used in a Rails 3 has_and_belongs_to_many context). Model and Table Setup My models are Foo and Bar and per rails convention, I have a join table called bars_foos . There is no primary key or timestamps making the old fields in this table bar_id:integer and foo_id:integer . I'm interested in knowing which of the following indexes is best and is without duplication: A compound