Tag: activerecord

如何使用Ruby on Rails查找今天,昨天等的记录?

我想找到所有的记录,比如Posts,今天用Ruby on Rails创建,然后是昨天创建的所有post,等等……我该怎么办? 谢谢, 凯文

有没有办法在后续的Rails 3 ActiveRecord迁移中删除id列?

在Rails 3中,ActiveRecord create_table可以包含选项:id => false。 例如 create_table :posts, :id => false do |t| … end 但是可以在后续向上迁移中删除现有表上的:id列吗?

删除Rails中的关联模型

嗨,我想知道是否可以删除rails中的关联。 好吧,我有类似的东西: class Home < ActiveRecord::Base include settings end 在settings.rb上,我有类似的东西 module Settings attr_reader :person attr_reader :address def self.included(base) base.belongs_to :city base.belongs_to :entity […] end […] end 在家庭课上,我的特定情况下的城市模型协会没有意义。 我必须找到一种方法来删除它以维护我的代码DRY。 谢谢! 注意:请在投票前给我反馈。

ActiveRecord布尔validation接受非布尔值

我正在尝试validation属性是否为布尔值,即true或false。 从我预期的Rails指南 validates :new_out_of_stock, inclusion: { in: [true, false] } 工作,但它接受非布尔值(例如“Hi”)作为有效: irb> ip = ItemPrice.new irb> ip.new_out_of_stock = “Hi” => “Hi” irb> ip.valid? => true irb> ip.errors.messages => {} 它正确拒绝nil并接受真假。 我读过的所有Internet上的文档都说这是validation布尔属性的正确方法。 为什么不起作用? 编辑:这是类定义: class ItemPrice < ActiveRecord::Base belongs_to :item, counter_cache: true validates :new_out_of_stock, inclusion: { in: [true, false] } end

在rails中在运行时向模型添加列(无需其他表)

我正在尝试为我的Web应用程序的管理员提供向模型添加一些新字段的function。 该模型称为Artwork,我想在运行时为instante添加一个test_column列。 我只是在讨论,所以我添加了一个简单的链接来实现它,它当然是参数化的。 我设法通过迁移来实现: def test_migration_create Artwork.add_column :test_column, :integer flash[:notice] = “Added Column test_column to artworks” redirect_to :action => ‘index’ end def test_migration_delete Artwork.remove_column :test_column flash[:notice] = “Removed column test_column from artworks” redirect_to :action => ‘index’ end 它可以工作,从数据库中添加/删除列没有问题。 我现在正在使用active_scaffold,所以我在表单中获得test_column字段而不添加任何内容。 但是,当我提交创建或更新时,test_column不会更新并保持为空。 检查参数,我可以看到: Parameters: {“commit”=>”Update”, “authenticity_token”=>”37Bo5pT2jeoXtyY1HgkEdIhglhz8iQL0i3XAx7vu9H4=”, “id”=>”62”, “record”=>{“number”=>”test_artwork”, “author”=>””, “title”=>”Opera di Test”, “test_column”=>”TEEST”, “year”=>””, “description”=>””}} test_column参数正确传递。 那么为什么活跃的记录一直无视呢? […]

在Rails ActiveRecord中,连接在命名空间模型中不能与has_and_belongs_to_many一起使用

我在命名空间中有两个模型,一个服务和一个教师,两者之间有很多对多的关系,通过has_and_belongs_to_many定义: class Scheduling::Service < ActiveRecord::Base has_and_belongs_to_many :instructors end class Scheduling::Instructor {:id => service_id})} has_and_belongs_to_many :services, :class_name => “Scheduling::Service” end 在of_service的范围内,我希望of_service返回与服务关联的所有教师。 但是,运行此范围时,我收到一个错误: ActiveRecord :: StatementInvalid:PG ::错误:错误:表“服务”缺少FROM子句条目LINE 1:….“id”=“instructors_services”。“service_id”WHERE“services”… ^:SELECT “scheduling_instructors”。* FROM“scheduling_instructors”INNER JOIN“instructors_services”ON“instructors_services”。“instructor_id”=“scheduling_instructors”。“id”INNER JOIN“scheduling_services”ON“scheduling_services”。“id”=“instructors_services”。“service_id “WHERE”服务“。”id“= 107 LIMIT 1 似乎出错的地方是它加入了一个名为instructors_services的表(该表不存在),忽略了相关模型中的命名空间。 它应该加入一个名为scheduling_instructors_services的表,该表与命名空间一致。

如何在Active Record中检索批量插入的已创建ID列表?

我有三个型号: class Coupon :destroy has_many :events, :through => :coupon_events end class Event :destroy has_many :coupons, :through => :coupon_events end class CouponEvent < ActiveRecord::Base belongs_to :coupon belongs_to :event end 我通读了一个CSV文件来创建优惠券和coupon_events。 这非常低效,因为记录一次创建一个并导致多个查询,每个查询包括两个插入语句。 我想使用这样的单个插入查询: coupon_string = ” (‘abc’,’AAA’), (‘123′,’BBB’)” Coupon.connection.insert(“INSERT INTO coupons (code, name) VALUES”+coupon_string) 然后我需要为CouponEvent模型创建第二个插入查询,但我需要一个返回的coupon_ids列表。 是否有内置方法在插入时检索ID?

优化困难查询(可能带有吱吱声)

有这样的代码(使用PublicActivity gem&Squeel) def index @activities = Activity.limit(20).order { created_at.desc } @one = @activities.where{trackable_type == ‘Post’}.includes(trackable: [:author, :project]) @two = @activities.where{trackable_type == ‘Project’}.includes trackable: [:owner] @activities = @one + @two end 但它创建了8个 SQL请求: SELECT “activities”.* FROM “activities” WHERE “activities”.”trackable_type” = ‘Post’ ORDER BY “activities”.”created_at” DESC LIMIT 20 SELECT “posts”.* FROM “posts” WHERE “posts”.”id” IN (800, 799, […]

postgresql,奇怪的OFFSET / LIMIT行为(记录顺序)

所以基本上我有这个范围(sql): scope.to_sql => “SELECT \”offers\”.* FROM \”offers\” INNER JOIN \”cities_offers\” ON \”offers\”.\”id\” = \”cities_offers\”.\”offer_id\” WHERE \”cities_offers\”.\”city_id\” = 2 AND \”offers\”.\”category_id\” IN (2) AND (offers.category_id is NOT NULL) ORDER BY offers.discount desc LIMIT 25 OFFSET 0″ 不知何故,上述查询的记录顺序不同,而且没有LIMIT和OFFSET的记录顺序不同: scope[6] => # #<Offer id: 8729 … 8629和8729记录都具有相同的discount值(我订购的属性)。 如果可以在这些情况下保持相同的记录顺序,请您提供建议吗?

Rails has_many:虽然是STI

让我们从代码开始,可能是这样的: class User < ActiveRecord::Base has_many :photos has_many :submitted_photos has_many :posted_photos has_many :handshakes, through: :photos end class Photo < ActiveRecord::Base belongs_to :user end class PostedPhoto < Photo has_many :handshakes, inverse_of: :poster, foreign_key: 'poster_id' end class SubmittedPhoto < Photo has_many :handshakes, inverse_of: :submitter, foreign_key: 'submitter_id' end class Handshake < ActiveRecord::Base belongs_to :submitter, class_name: 'SubmittedPhoto' belongs_to :poster, […]