Tag: rails activerecord

undefined方法`group_by_day’ – rails 3.2

我想使用chartkick制作一个用户注册数量(每天)的折线图 ,这是一个简单的一行代码,但它给出了一个错误 `undefined method `group_by_day’ for #` 在我看来,我有 错误日志: Rendered admin/dashboards/index.html.erb within layouts/application (145.2ms) Completed 500 Internal Server Error in 1018ms NoMethodError – undefined method `group_by_day’ for #: activerecord (3.2.14) lib/active_record/dynamic_matchers.rb:55:in `method_missing’ app/views/admin/dashboards/index.html.erb:38:in `_app_views_admin_dashboards_index_html_erb___3330517857505238097_39214860′ actionpack (3.2.14) lib/action_view/template.rb:145:in `block in render’ activesupport (3.2.14) lib/active_support/notifications.rb:125:in `instrument’ actionpack (3.2.14) lib/action_view/template.rb:143:in `render’ actionpack (3.2.14) lib/action_view/renderer/template_renderer.rb:48:in `block (2 levels) in […]

在Rails中添加可以为空的外键

引用Rails 4.2 add_foreign_key支持: # add a foreign key to `articles.author_id` referencing `authors.id` add_foreign_key :articles, :authors 如何创建一个可以为空的外键约束,以允许这样的情况,其中articles.author_id有时可以为null?

为什么alias_method在Rails模型中失败

class Country < ActiveRecord::Base #alias_method :name, :langEN # here fails #alias_method :name=, :langEN= #attr_accessible :name def name; langEN end # here works end 在第一次调用alias_method失败时: NameError: undefined method `langEN’ for class `Country’ 我的意思是当我做例如Country.first时它失败了。 但是在控制台中我可以成功调用Country.first.langEN ,并看到第二个调用也可以。 我错过了什么?

如何在一个表中添加对同一模型的多个引用的迁移? 的Ruby / Rails

如何使用引用同一个表的两个字段创建迁移? 我有表A和图像。 A.image1_id将参考图像,而A.image2_id也将参考图像。 只有2张图片,不是很多。 如果我使用 class AddFields < ActiveRecord::Migration def change change_table(:ticket) do |t| t.references :image1_id t.references :image2_id end end end 我认为这不会起作用,因为它会在最后添加另一个_id,并且可能不会知道使用’image’模型。 我也想过 change_table(:ticket) do |t| t.references :image 但是,我该如何添加其中两个呢? 我也考虑过添加 create_table :images do |t| t.belongs_to :ticket t.string :file 但我只想要2,而不是很多,这似乎不允许从票证中获取图像,如ticket.image1或ticket.image2 。 根据这个文档http://apidock.com/rails/v3.2.8/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table这是我能找到的全部内容,t.references似乎也没有任何参数。 change_table(:suppliers) do |t| t.references :company end

如何在rails中添加条件where子句

我是一个铁杆新手,我正试图在带有rails的桌面上进行搜索,而我只是使用我的sql知识来做到这一点。 但这似乎不像铁路或ruby甚至…… 有没有更好的方法来做我正在做的事情? (基本上,只有在填充时才将日期参数传递给sql) def search(begin_date=nil, end_date=nil) subject = ” and created_at ” if !(begin_date.nil? || end_date.nil?) where_part = subject + “BETWEEN :begin_date AND :end_date” else if (begin_date.nil? && end_date.nil?) where_part = “” else if(begin_date.nil?) where_part = subject + ” = :begin_date” end end end end User.joins(places: {containers: {label: :user}}).where(“users.id= :user_id “+where_part, user_id: self.id, begin_date:begin_date, […]

在Rails应用程序中的查询运行时更改表名

我在Apache + mod_passenger上有一个胖的多租户Rails应用程序,它从PostgreSQL表中输出产品价格,如下所示: Table “public.products” Column | Type id | bigint name | character varying(100) price | numeric(8,2) 然后在products.rb我有…… class Product < PostgresDatabase self.table_name = "products" # … yadda yadda end 我想要的是以非常具体的方式对“产品”表进行分区,以便最终为每个租户提供类似product_TENANT-ID的内容(基本上是主要产品表的视图,但这是另一个故事),并且能够像这样查询: Products.for_tenant(TENANT-ID).where(:name => “My product”)…… 我想我可以创建一个方法: class Product < PostgresDatabase self.table_name = "products" # … yadda yadda def for_tenant(tid) self.table_name = "products_" + tid.to_s […]

我可以在Ruby on Rails上编写PostgreSQL函数吗?

我们正在开始一个基于Ruby on Rails的项目。 我们曾经使用Perl和PostgreSQL函数,并且使用Rails和Active Record我没有看到我们应该如何在PostgreSQL中创建函数并保留Active Record和模型的记录。 我知道我们可以在PostgreSQL中手动创建它,但Active Record的“魔力”在于可以使用所有模型重新创建数据库。 有没有办法使用Rails创建PostgreSQL函数并将其保存在模型中?

将多个命名范围与OR组合

我正在尝试组合两个范围或向现有范围添加更多范围。 scope :public_bids, -> { where(status: [Status::ACCEPTED, Status::OUTBID, Status::SOLD, Status::NO_SALE], bid_type: [BidType::MANUAL, BidType::AUTO, BidType::LIVE]) scope :outbid_maxbids, -> { where(status: Status::OUTBID, bid_type: BidType::MAXBID) 我无法确定如何将它们组合在一起或者将它们合并到一个范围内。 有什么建议/指导吗? 我希望将它们组合成一个单一的范围。

返回自定义查询在activerecord中选择

我有一个查询,它做了一些数学运算,并返回一个带有结果集的计算自定义选择字段。 我无法弄清楚如何在返回的activerecord对象中访问它。 我也为它添加了一个attr_accessor。 attr_accessor :percentage_used select(‘gateways.*, (num_transactions_today/ SUM(num_transactions_today)) AS percentage_used ‘).joins(:gateway_groups).where(‘map_gateway_groups.gateway_group_id = ?’, gateway_group_id) 在结果集中,我希望能够访问:percentage_used,但它不在那里。 关于我做错什么的任何想法? 我以前从未需要这样做。 谢谢

查询,无法选择列数

Tag.joins(:quote_tags).group(‘quote_tags.tag_id’).order(‘count desc’).select(‘count(tags.id) AS count, tags.id, tags.name’) Build query: SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc 结果: [#, … , #] 它不会为我返回计数列。 我怎么才能得到它?