我试图创建以下内容:
Models:User,Account,Transaction Logic:User有多个帐户。账户有很多交易。用户通过帐户进行许多事务处理。
模型
class User < ApplicationRecord
has_many :accounts
has_many :transactions, :through => :accounts
End
class Account < ApplicationRecord
belongs_to :user
has_many :transactions
end
class Transaction
我的一个模型表(事件)没有显示在schema.rb文件中,取而代之的是下面的消息-
# Could not dump table "events" because of following NoMethodError
# undefined method `[]' for nil:NilClass
最后一次成功的迁移如下所示-
class RemoveOrganiserDescriptionFromEvents < ActiveRecord::Migration
def change
remove_column :events, :organise
因此,我遵循了这个关于如何使用Sinatra:设置postgres db的好教程。
我已经按照建议设置了我的environments.rb,并设置了rake文件,如下所示:
configure :development do
set :database, "sqlite:///dev.db"
set :show_exceptions, true
end
configure :production do
db = URI.parse(ENV["DATABASE_URL"] || 'postgres:///localhost/mydb'
我正在努力学习rails的活动记录。了解到self.table_name可以用于重命名rails创建默认的表。
class Order < ActiveRecord::Base
self.table_name = "ordered"
end
但是当我第一次运行迁移时,我仍然会得到orders表,而不是我在字符串中提到的那个表。这里做错了什么?
移徙:
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.timest
class UpdateIndexOnUsers < ActiveRecord::Migration
def change
sql = 'DROP INDEX index_users_on_email'
sql << ' ON users' if Rails.env == 'production' # Heroku pg
ActiveRecord::Base.connection.execute(sql)
end
end
我必须撤销这次迁徙。当我使用rake db:migrate VERSION=2015
我收到了下面的错误消息,不知道如何解决它。有人能帮忙吗?
NoMethodError:未定义的方法`needs_migration?对于ActiveRecord::Migrator:Class
以下是config.ru代码:
require './config/environment'
if ActiveRecord::Migrator.needs_migration?
raise 'Migrations are pending. Run `rake db:migrate` to resolve the issue.'
end
use Rack:
我的应用程序很简单,我有User,它可以有许多Videos,而Video是多到多到多的Tag。
这是我的模型
class User < ActiveRecord::Base
def authenticate
return true
end
end
class Video < ActiveRecord::Base
belongs_to :user
end
class Tag < ActiveRecord::Base
end
class VideoTag < ActiveRecord::Base
belongs_to :vide
我有许多使用相同列名的表。我想把它们分成多个部分,这样我就可以在每次重新创建数据库时编辑这些部分以更改列。
我试过了:
水果迁移文件
class CreateFruit < ActiveRecord::Migration[5.0]
def change
create_table :fruits do |t|
partial
end
end
pet迁移文件
class CreatePet < ActiveRecord::Migration[5.0]
def change
create_table :pets do |t|
par
我试图使用载波上传多个图像,当试图保存时,我得到了这个错误。
can't cast Array to
告诉我控制器中有错误
if @property.save
我不知道它是否相关,但是当我添加这个迁移时
rails g migration add_images_to_properties images:json
模式为属性表提供了这个问题。
# Could not dump table "properties" because of following NoMethodError
零的未定义方法‘[]:NilClass
其他一切都井井有条..。
class Proper
我正在开发一个遵循的多租户应用程序。问题是当我第一次运行所有迁移时。在schema.rb文件中,只有公共模式的表,但是其他模式会发生什么呢?以及如何创建与我不想使用的public不同结构的其他模式。
参见下面的示例
要为公共模式创建的表
class CreatePerspectives < ActiveRecord::Migration
include MultiSchema
def up
with_in_schemas :only => :public do
# Create table perspectives
end
en
在这个应用程序中,我正在尝试进行图像上传。当我运行迁移时,我得到了错误。
控制器:
class PostsController < ApplicationController
def index
end
def new
@post = Post.new
end
def
@post = Post.new(post_params)
@post.save
end
def show
@post=Post.find(params[:id])
end
def edit
end
def delete
end
我有以下迁移和模型:
class CreatePlatforms < ActiveRecord::Migration
def change
create_table :platforms do |t|
t.integer :user_id
t.string :name
t.string :platform_id
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
creat
我正在构建一个向ActiveRecord::Migration添加功能的库,但我正在努力理解它的行为。 加载库时,我执行以下代码 ActiveSupport.on_load(:active_record) do
ActiveRecord::Migration.prepend MyLibrary::Mutators
end 然后在my_library/mutators.rb中 module MyLibrary
module Mutators
def do_something
# do stuff here and use `self`
end
end
e
我想在CriminalRecord.和用户之间创建一个简单的belongs_to关联。用户只有一条记录,一条记录属于用户。到目前为止,我在相应的文件中有以下内容。
模型> user.rb
class User < ActiveRecord::Base
end
模型> criminal_record.rb
class CriminalRecord < ActiveRecord::Base
belongs_to :user
end
db >迁移> 20150902003211_create_criminal_records
class CreateCrimi
当我试图为这个文件运行迁移时:
class AddIndexToUsernameDowncaseForUsers < ActiveRecord::Migration
def up
execute 'CREATE INDEX index_users_on_username_downcase ON users (lower(username));'
end
def down
remove_index :users, name: :index_users_on_username_downcase
end
end
我知道这个错误:
== 2014
嗨,我有一个关于rails协会的问题。我有表用户和表角色。以下是我的迁移:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :email
t.string :password_digest
t.belongs_to :role, index: true, foreign_key: true
t.timestamps
end
end
end
class CreateRol
这些是我对表users_hobbies的迁移
创建:
Sequel.migration do
up do
create_table :users_hobbies do
Integer :user_id
Integer :hobby_id
end
end
down do
drop_table :users_hobbies
end
end
改变:
Sequel.migration do
up do
alter_table :users_hobbies do
unique [:user_id, :hobby_i
通过在迁移名称中指定关键字'Create‘,可以从命令行调用create_table语法:
rails g migration CreateMyTables name:string
这将创建一个包含以下内容的迁移:
class CreateMyTables < ActiveRecord::Migration
def change
create_table :my_tables do |t|
t.string :name
end
end
end
我想用'Change‘关键字来完成同样的事情。因此,我将从命令行运行:
rails g mig
我在rails 4中的论坛应用程序中遇到了关联问题,这可能很简单,但我找不到任何解决我的问题的解决方案(我也是rails新手)。这是论坛的层次结构:主题>主题>帖子(类似评论)
我想要的是,在“主题秀”中,检索它自己的帖子。
在我的模特身上
#Theme model
class Theme < ActiveRecord::Base
has_many :topics, dependent: :destroy
has_many :posts
end
#Topic model
class Topic < ActiveRecord::Base
belo
使用bin/rails g migration CreateUser生成新迁移时,第一行如下所示:
class CreateUser < ActiveRecord::Migration[5.1]
[5.1]代表什么?它是如何工作的?
这是对的后续,因为它没有解释这是如何合法的红宝石,也没有出现在搜索使用[5.1]或brackets。
当前正在尝试将rolify安装到我的rails应用程序
按照github上的步骤操作,在运行rails db后得到以下错误:migrate
/home/alex/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rolify-5.2.0/lib/rolify.rb:30: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/alex/.rbenv/versions/2.7.1/l
我正在做一个带有RoR服务器的小安卓项目。以下是三种模型:
class User < ActiveRecord::Base
has_many :relations
has_many :friends, :through => :relations
attr_accessor :friend_ids
end
class Relation < ActiveRecord::Base
belongs_to :user
belongs_to :friend
end
class Friend < ActiveRecord::Bas
我正在尝试更新一个名为的gem,使其与最新的rails版本兼容。
在开发Rake任务中,我们需要运行一些没有Rails的ActiveRecord迁移。
今天看来:
namespace :migrate do
desc 'Run the test database migrations'
task :up => :'db:connect' do
ActiveRecord::Migrator.up 'db/migrate'
end
desc 'Reverse the test databas
以下是错误:
移民法:
class Test < ActiveRecord::Migration
def change
create_table :test do |t|
end
end
end
另外,如果删除"Create_table“调用,它不会失败。
在运行rake db:migrate时,我得到以下错误信息:
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `each_pair' for []:Ar