Tag: activerecord

Ruby – 确保只有一个类对象

我有一个Model Bot ,我想确保我的数据库中只有一个Bot对象。 我还需要确保它是持久的而不是被篡改的。 我最初的想法是在迁移中执行此操作,该操作将遵循:bots表迁移。 它将包括一行如下: Bot.all.size == 0 ? Bot.create! : nil 也许这会阻止AR对象在将来的迁移中被搞乱? 奖励:能够对此类对象进行即时和全局访问真是太棒了。 我正在考虑在我的Bot类中使用singleton模块,这样我总是可以引用Bot.instance并且可以访问该特定对象。 使用案例: 我的数据库中有4种类型的用户,这个机器人将通过我们的应用内消息传递function为他们提供特定于角色的消息。 Class Bot将与BotMessage/bot_messages有一个has_many关联。 在bot_messages表上将是user_role的枚举字段。 消息将由公司管理员创建并存储在这些表中,因为我们希望通过查看用户和Bot之间的“对话”线程随时查看这些消息。 当谈到只有1个机器人时,就是这样。 我不需要额外的Bot对象。 此外,由于只有一个对象,因此能够有一种明确定位该对象的方法而不必运行查询来查找它。 例如,与可能有1000条记录的User不同,为了找到特定的记录,你会做类似@user = User.find_by_email(‘foo@bar.com’)事情,对机器人做类似的事情是没有必要的。因为只有一条记录可供查找。 这就是让我相信在这里有一个singleton对象可能是值得的,因此每当我需要为特定角色提取消息时,我可以运行Bot.instance.bot_messages.where(user_role: 1)或类似的东西

错误:表“用户”上的更新或删除违反表“评论”上的外键约束“fk_rails_03de2dc08c”

我坚持这个错误,无法弄清楚什么是错的。 铁轨已中止! ActiveRecord :: InvalidForeignKey:PG :: ForeignKeyViolation:错误:表“user”上的更新或删除违反了表“comments”上的外键约束“fk_rails_03de2dc08c”DETAIL:Key(id)=(11)仍然从表“comments”引用。 :DELETE FROM“users”WHERE“users”。“id”= $ 1#31

rails中的交叉唯一性模型validation

我有一个模型,我希望列包含不同的ID。 因此,用户可以跟随另一个用户,但不跟随他们自己。 移民 class CreateFollows < ActiveRecord::Migration def change create_table :follows do |t| t.integer :follower_id t.integer :followee_id t.timestamps end end end 模型 class Follow < ActiveRecord::Base belongs_to :follower, class_name: 'User' belongs_to :followee, class_name: 'User' validates :follower_id, uniqueness: { scope: :followee_id } end 但我的测试似乎失败了 测试 it ‘cannot follow itself’ do user = FactoryGirl.create(:user) follow = FactoryGirl.create(:follow, […]

mongodb中的多重限制条件

我有一个集合,其中一个字段是“类型”。 我希望得到每种类型的一些值,具体取决于所有类型相同的条件。 就像我想要类型A的2个文档,类型B的2个文档。 如何在单个查询中执行此操作? 我正在使用Ruby Active Record。

如何在两个相互依赖的属性上运行回调函数?

在我的Rails应用程序中,我有一个Invoice模型,其属性为date和due_date 。 为简单起见,我不希望用户手动输入due_date ,而只需输入应添加到date 的天数 。 这就是我设置虚拟属性days_allowed 。 class Invoice < ActiveRecord::Base belongs_to :user before_save :save_date attr_accessor :days_allowed def days_allowed # attribute reader (I need that too!) (due_date – date).to_i end def save_date self.due_date = date + days_allowed.days end end 但是,当用户在表单中选择日期并输入天数(例如10天)时,我得到错误的结果,因为save_date回调引用了days_allowed方法而不是同名属性。 关键问题似乎是我在两个相互依赖的不同属性( date和days_allowed )上使用回调。 谁能告诉我如何解决这个难题? 谢谢你的帮助。

派生的Activerecord Ruby类

我对ruby完全不熟悉。 我正在尝试为任务跟踪应用程序创建RESTful服务。 我研究并发现Sinatra比铁路更好。 所以我使用的是Sinatra和ActiveRecord。 我正在跟随Sinatra和ActiveRecord运行 。 我将使用Restsharp在.NET中创建客户端应用程序。 但这都是关于服务器端的。 这是我创建的迁移 class CreateTasksPeopleDocumentsAndComments < ActiveRecord::Migration def self.up create_table :tasks do |t| t.string :name t.string :task_status t.string :task_type t.text :description t.text :analysis t.text :investigation t.integer :developer_id t.integer :reviewer_id t.date :open_date t.date :analysis_date t.date :promotion_date t.date :review_date t.date :correction_date t.date :collection_date t.date :closed_date t.date :modified_date t.date :target_date end create_table […]

ActiveRecord :: Base的未定义方法`mysql_connection’:Class

我已经将rails2升级到rails3,当我尝试rake db:migrate时,我发现了以下错误 `rake aborted!` undefined method `mysql_connection’ for ActiveRecord::Base:Class /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/dynamic_matchers.rb:55:in `method_missing’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/ar-octopus-0.6.1/lib/octopus/proxy.rb:153:in `select_connection’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/ar-octopus-0.6.1/lib/octopus/proxy.rb:215:in `method_missing’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/migration.rb:662:in `initialize’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/migration.rb:570:in `new’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/migration.rb:570:in `up’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/ar-octopus-0.6.1/lib/octopus/migration.rb:117:in `up_with_octopus’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/activerecord-3.2.14/lib/active_record/migration.rb:551:in `migrate’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/ar-octopus-0.6.1/lib/octopus/migration.rb:111:in `block in migrate_with_octopus’ /home/cis/.rvm/gems/ruby-1.9.3-p392@rails3214/gems/ar-octopus-0.6.1/lib/octopus/proxy.rb:171:in `run_queries_on_shard’ […]

当我尝试做rake db:migrate时,我收到一个错误:ActiveRecord :: NODatabaseError角色“ubuntu”不存在

我通过这样做启动了postgresql服务器: sudo service postgresql start 然后我连接到服务: sudo sudo -u postgres psql 然后我创建了一个数据库(我正在尝试为我的应用添加一个投票系统): postgres=# CREATE DATABASE “votes”; 但我仍然有同样的问题。 还有,当我这样做的时候 rake db:create 我得到一个角色“ubuntu”不存在错误 这是我的database.yml: # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem ‘sqlite3’ # # default: &default # adapter: sqlite3 # pool: 5 […]

Ruby on Rails – 显示has_many表中的字段

我有两张桌子:船和航程。 一艘船可以有很多航程,但是一次航行可能只有一艘船。 在RoR中,船模型具有has_many:voyages,Voyage模型设置为belongs_to:ship 我可以使用与此类似的代码在视图中显示ship表中的所有字段而不会出现任何问题: 我现在正试图在视图中显示航行表中的一条信息。 如果我做: 我能够拉出ActiveRecord条目,即: # 如果我追加.to_json,我可以提取包含所有数据的json文件。 如何在我的视图中显示特定字段,我尝试过的内容包括: 和 但两个错误都在我身上。 注意:虽然这种关系是一艘船到多次航行 – 目前每艘船只有一次航行。 我承认自己是一个新手!

访问活动记录关系时出现奇怪错误

我有这些模型: class Segment :segment_code, :primary_key => :code end class Family :segment_code, :primary_key => :code has_many :genus, :foreign_key => :family_code, :primary_key => :code end class Genus :family_code, :primary_key => :code has_many :commodities, :foreign_key => :genus_code, :primary_key => :code end class Commodity :genus_code, :primary_key => :code end 当我调用这个rails console irb命令序列时,我收到此错误,我失去了请帮助。 我也在想我可能需要使用很多:通过。 对我的数据模型的批评也很受欢迎。 这是错误: NameError: uninitialized constant Family::Genu […]