Tag: activerecord

将关联从一个更改为多个到多个到多个

我想有两个模型 class A < ActiveRecord::Base has_many :bs end class B < ActiveRecord::Base belogns_to :a end 现在因为一些系统更改,我需要将这种关联转换为很多很多这样的事情 class A < ActiveRecord::Base has_and_belongs_to_many :bs end class B < ActiveRecord::Base has_and_belongs_to_many :as end 要么 class A < ActiveRecord::Base has_many :cs has_many :bs, through: :cs end class B < ActiveRecord::Base has_many :cs has_many :as, through: :cs end class C […]

如何从Rails模型中获取属性的数据类型?

我在Rails项目中使用Postgres,我发现我需要将所有varchar数据类型更改为citext 。 我想要创建一个循环遍历所有模型及其属性的迁移,并根据需要转换它们,而不是手动执行此操作。 大多数这些模型都是空的,因此不是实例化它们的问题。 我需要找出ActiveRecord是否“知道”其相应数据库列的数据类型是什么。

使用end作为列名

我正在维护一个rails 2.1应用程序,它对列名称有一些不幸的选择。 例如,一个事件有一个开始和结束日期。 原始设计使用start和end而不是使用end_at和end_at 。 当然这会导致 def end read_attribute(:end) || 1.hour.from_now end 我甚至惊讶于解析。 这是合法的ruby吗? 真正的问题是,当运行backgroundrb作业发送提醒电子邮件时,erb正在爆炸“堆栈级别太深”。 模板是 如果我在控制台中发送deliver_reminder没有错误,但是在后台作业期间调用deliver_reminder时,会发生错误。 问题:我应该重构删除end方法,还是堆栈错误是由其他原因引起的? 在foo / mailer / reminder.rhtml的#1行 1: lib/virtual_attributes_and_associations.rb:59:in `virtual_attribute_names’ lib/virtual_attributes_and_associations.rb:83:in `read_attribute_without_virtual’ lib/virtual_attributes_and_associations.rb:86:in `read_attribute’ vendor/rails/activerecord/lib/active_record/base.rb:2720:in `send’ vendor/rails/activerecord/lib/active_record/base.rb:2720:in `clone_attribute_value’ vendor/rails/activerecord/lib/active_record/dirty.rb:127:in `write_attribute’ vendor/rails/activerecord/lib/active_record/attribute_methods.rb:211:in `data=’ lib/virtual_attributes_and_associations.rb:9:in `included’ vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `call’ vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `evaluate_method’ vendor/rails/activesupport/lib/active_support/callbacks.rb:161:in `call’ vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run’ vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each’ vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send’ vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run’ vendor/rails/activesupport/lib/active_support/callbacks.rb:272:in […]

如何validation付款永远不会导致发票金额小于零?

我有这堂课: class Payment { :greater_than => 0, :less_than_or_equal_to => :maximum_amount } after_save :update_amount_payable after_destroy :update_amount_payable private def maximum_amount invoice.amount_payable end def update_amount_payable invoice.update_amount_payable end end class Invoice < ActiveRecord::Base has_many :payments after_save :update_amount_payable def update_amount_payable update_column(:amount_payable_in_cents, new_amount_payable) end private def new_amount_payable (total – payments.map(&:amount).sum) * 100 end end 上面的代码有效。 但是,如何确认没有付款金额可以导致invoice.amount_payable小于0 ? 特别是当同一张发票的多次付款可能时,这就变得棘手了。 我一直试图让我的头围绕这几个小时,但无济于事。 也许回滚数据库的回调可以在这里使用? 谢谢你的帮助。

设计:新用户注册问题,PG :: ProtocolViolation。 Rails4,Postgres

我正在使用devise和omni-auth登录linkedin。 我将from_omniauth方法添加到Railscast中提到的用户模型: 这是代码: #Create a new user if does not exist def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_create! do |user| user.provider = auth.provider user.uid = auth.uid user.email = auth.info.email end end Started GET “/users/auth/linkedin/callback?oauth_token=XXX” INFO — omniauth: (linkedin) Callback phase initiated. Processing by OmniauthCallbacksController#linkedin as HTML (0.4ms) BEGIN User Exists (0.6ms) SELECT 1 AS one FROM “users” WHERE […]

覆盖rails #destroy参数错误

我的目标: 覆盖rails中的#destroy方法,以便像Devise这样的gem将执行软删除。 我的尝试: def destroy(mode = :soft) if mode == :hard super else … soft-delete actions … end end 当它在对象上被调用时它得到… ArgumentError:参数个数错误(1表示0) 我想这个默认为软删除,但可以选择完全销毁对象。 我的方法可能不是最好的方法,但任何建议都会有所帮助。

JRuby上的Rails的ActiveRecord数据库gem是什么?

在JRUBY上运行rails时,数据库适配器有两个不同的gem。 Sql Server : sqlserver gem和activerecord-sqlserver-adapter gem Mysql : jdbcmysql gem vs activerecord-jdbcmysql-adapter gem Sqlite3 : jdbcsqlite3 gem和activerecord-jdbcsqlite3-adapter gem Postgresql : jdbcpostgresql gem Vs activerecord-jdbcpostgresql-adapter gem jdbc : jdbc gem vs activerecord-jdbc-adapter gem 所以问题是, jdbcpostgresql和它的长格式activerecord-jdbcpostgresql-adapter gem有什么activerecord-jdbcpostgresql-adapter ?

rails sum多个字段

我试图在rails中做这个假设简单的操作: self.timesheets.select(“sum(total) as total, sum(quantity) as quantity”).first where self is a project 当我在控制台模式下执行它时,它可以工作,但它呈现3列: [#] 当我在应用程序中运行它时,我收到此错误消息: PG::GroupingError – ERROR: column “timesheets.id” must appear in the GROUP BY clause or be used in an aggregate function LINE 1: …” WHERE “timesheets”.”project_id” = $1 ORDER BY “timesheet… ^ : activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:811:in `prepare_statement’ activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:772:in `exec_cache’ schema_plus […]

Ruby on Rails:为什么当我向DB写一个时间时,我会获得时区,然后再读回来?

我将environment.rb中的config.time_zone设置为“UTC”,当我发出“select now();”时,我的mySQL服务器返回当地时区的当前时间。 当我要求“select utc_timestamp”时,在utc中 我正在运行rails 2.1.2,mysql gem 2.7.3,activerecord gem 2.1.2和mysql –version返回“Ver 14.12 Distrib 5.0.27 for Win32(ia32)”。 编辑:我的environment.rb设置为UTC,自从我启动项目以来。 服务器重启不会发生任何变化。 record = Record.find(:first) puts Time.now # Tue Nov 25 17:40:48 -0800 2008 record.time_column = Time.now record.save mysql> select * from records; ——————— 2008-11-26 01:40:48 #note that this is the same time, in UTC. record = Record.find(:first) puts […]

Rails – SQL查询返回所有指定相关对象的对象(has_many through)

这是我的第一个问题。 我想构建一个查询,它将为我提供具有所有指定function的所有主题。 我有三个模型主题,function和ThemeFeatures 主题has_many:features,:through =>:theme_feautures 可以说DB中有一个Theme对象(id:1,name:“theme_a”)。 它有(许多)function[(id:1,name:“feature_a”),(id:2,name:“feature_b”),(id:3,name:“feature_c”)] 查询应该像这样工作:Theme.the_query(:features => {:id => [1,2]}),结果是主题,但是当我以这种方式放置ID时Theme.the_query(:features => {:id => [2,4]})结果应为零。 我已经构建了Theme.joins(:features).where(:features => {:id => features_array_ids})但它返回了所有具有features_array_ids元素的主题。 如果features_array_ids = [2,4]它返回主题,但我不希望这样。 对于语法错误我很抱歉,我希望你知道我的意思。 编辑: 找到解决方案 Theme.select(“*”).from(“themes”).joins(“INNER JOIN theme_features ON themes.id = theme_features.id”).where(“EXISTS(SELECT theme_id FROM theme_features tf WHERE tf.feature_id IN (#{features_array_ids.join(“, “)}) AND tf.theme_id = themes.id GROUP BY tf.theme_id HAVING COUNT(*) = #{features_array_ids.count})”)