activerecord

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

拟墨画扇 提交于 2019-12-20 21:24:02
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

折月煮酒 提交于 2019-12-20 21:23:26
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

How would you do this Rails sub-query using Squeel?

喜夏-厌秋 提交于 2019-12-20 20:03:54
问题 I want to restructure the query below using Squeel. I'd like to do this so that I can chain the operators in it and re-use the logic in the different parts of the query. User.find_by_sql("SELECT users.*, users.computed_metric, users.age_in_seconds, ( users.computed_metric / age_in_seconds) as compound_computed_metric from ( select users.*, (users.id *2 ) as computed_metric, (extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds from users ) as users") The query

How would you do this Rails sub-query using Squeel?

时间秒杀一切 提交于 2019-12-20 20:03:27
问题 I want to restructure the query below using Squeel. I'd like to do this so that I can chain the operators in it and re-use the logic in the different parts of the query. User.find_by_sql("SELECT users.*, users.computed_metric, users.age_in_seconds, ( users.computed_metric / age_in_seconds) as compound_computed_metric from ( select users.*, (users.id *2 ) as computed_metric, (extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds from users ) as users") The query

How to reset auto increment field in a ActiveRecord migration?

人盡茶涼 提交于 2019-12-20 17:47:30
问题 In my migration I have: def up MyModel.destroy_all MyModel.create!({:id=>1,:name=>'foo'}) MyModel.create!({:id=>2,:name=>'fooBar'}) MyModel.create!({:id=>3,:name=>'fooNull'}) end because I need to override data that was already on my_models table But Even though I'm specifying the id on MySQL it continues the numbering from the position it already was. I need to rest the counter on the auto increment for id to have only this 3 new records with that id values trough Active Record migration on

Rails includes with scope

时光总嘲笑我的痴心妄想 提交于 2019-12-20 17:37:48
问题 I have a model called Author. An author has many Articles. Articles have a scope called .published that does: where(published: true). I want to load the author, with the published articles. I tried: Author.includes(:articles.published).find(params[:author_id]) But that throws an error: undefined method 'published'. Any idea? 回答1: I think the best solution would be: Author.includes(:articles).where(:articles=>{published: true}).find(params[:author_id]) Or you can create scope: class Author <

Track dirty for not-persisted attribute in an ActiveRecord object in rails

邮差的信 提交于 2019-12-20 16:30:48
问题 I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like 'bar_changed?', as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs, I'm getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods , but with different

Track dirty for not-persisted attribute in an ActiveRecord object in rails

纵饮孤独 提交于 2019-12-20 16:30:31
问题 I have an object that inherits from ActiveRecord, yet it has an attribute that is not persisted in the DB, like: class Foo < ActiveRecord::Base attr_accessor :bar end I would like to be able to track changes to 'bar', with methods like 'bar_changed?', as provided by ActiveModel Dirty. The problem is that when I try to implement Dirty on this object, as described in the docs, I'm getting an error as both ActiveRecord and ActiveModel have defined define_attribute_methods , but with different

Trouble with accepts_nested_attributes_for on validating foreign key

浪尽此生 提交于 2019-12-20 16:23:33
问题 I am using Ruby on Rails v3.2.2. I would like to solve the issue related to the validation of a foreign key when using accepts_nested_attributes_for and validates_associated RoR methods. That is, I have following model classes: class Article < ActiveRecord::Base has_many :category_associations, :foreign_key => 'category_id' accepts_nested_attributes_for :category_associations, :reject_if => lambda { |attributes| attributes[:category_id].blank? } validates_associated :category_associations end

Possible to specify unique index with NULLs allowed in Rails/ActiveRecord?

拟墨画扇 提交于 2019-12-20 16:19:52
问题 I want to specify a unique index on a column, but I also need to allow NULL values (multiple records can have NULL values). When testing with PostgreSQL, I see that I can have 1 record with a NULL value, but the next will cause an issue: irb(main):001:0> u=User.find(5) User Load (111.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 5]] => #<User id: 5, email: "a@b.com", created_at: "2013-08-28 09:55:28", updated_at: "2013-08-28 09:55:28"> irb(main):002:0> u.email=nil