activerecord

Triggering dependent: :destroy with overridden destroy-method

旧街凉风 提交于 2020-01-31 10:35:03
问题 In our application we've overridden the ActiveRecord destroy method so that our records do not get deleted (so the user can undelete). Like so: def destroy self.is_deleted = true self.save freeze end However, this seems to have disabled the dependent destroy on our has_many relationships. In other words, if destroy is called on a parent object, the child objects of has_many do not get destroyed (they don't get deleted, i.e, SQL ' DELETE... ', nor is the overridden destroy -method called). How

CollectionProxy vs AssociationRelation

白昼怎懂夜的黑 提交于 2020-01-29 04:33:54
问题 I am wondering about the difference between ActiveRecord::Associations::CollectionProxy and ActiveRecord::AssociationRelation . class Vehicle < ActiveRecord::Base has_many :wheels end class Wheel < ActiveRecord::Base belongs_to :vehicle end So if I do: v = Vehicle.new v.wheels # => #<ActiveRecord::Associations::CollectionProxy []> v.wheels.all # => #<ActiveRecord::AssociationRelation []> I have no idea what is the difference between them and why this is implemented this way? 回答1: ActiveRecord

CollectionProxy vs AssociationRelation

非 Y 不嫁゛ 提交于 2020-01-29 04:32:26
问题 I am wondering about the difference between ActiveRecord::Associations::CollectionProxy and ActiveRecord::AssociationRelation . class Vehicle < ActiveRecord::Base has_many :wheels end class Wheel < ActiveRecord::Base belongs_to :vehicle end So if I do: v = Vehicle.new v.wheels # => #<ActiveRecord::Associations::CollectionProxy []> v.wheels.all # => #<ActiveRecord::AssociationRelation []> I have no idea what is the difference between them and why this is implemented this way? 回答1: ActiveRecord

ActiveRecord: Update a record if exists else create?

别来无恙 提交于 2020-01-29 03:18:49
问题 Trying to implement an create if not exists else update record in Active Record. Currently using: @student = Student.where(:user_id => current_user.id).first if @student Student.destroy_all(:user_id => current_user.id) end Student = Student.new(:user_id => current_user.id, :department => 1 ) Student.save! What would be the correct way of updating the record if it exists or else create it? 回答1: You may be looking for first_or_create or something similar: http://guides.rubyonrails.org/v3.2.17

Why do ActiveRecord callbacks require instance variables or instance methods to be prefixed with self keyword?

ぐ巨炮叔叔 提交于 2020-01-28 04:48:49
问题 ActiveRecord has a few different callback methods used to simplify model logic. For example after_find and before_create methods. Consider this code example: class ExternalPrintingCard < ActiveRecord::Base belongs_to :user belongs_to :ph_user after_create :change_pin def change_pin self.user.randomize_printer_pin end def after_find return if self.card_status == false self.card_status = false if self.is_used_up? self.card_status = false if self.is_expired? self.save! end end If I remove all

How to override a column in Rails model?

安稳与你 提交于 2020-01-28 04:48:06
问题 I have a model for one of my database tables. I want to override the column name for that particular table. How would I achieve it. For example, let my table be called DUMMY and it has one column called col_a col_a 20 34 42 23 12 I would be doing a @dummy.col_a . Now this method should return me 0 for numbers ending with 0 and for everything else, it should return the original value. I could do that by defining a new method, but I want to override the column name itself. Please help. 回答1: You

Why do ActiveRecord callbacks require instance variables or instance methods to be prefixed with self keyword?

心已入冬 提交于 2020-01-28 04:46:26
问题 ActiveRecord has a few different callback methods used to simplify model logic. For example after_find and before_create methods. Consider this code example: class ExternalPrintingCard < ActiveRecord::Base belongs_to :user belongs_to :ph_user after_create :change_pin def change_pin self.user.randomize_printer_pin end def after_find return if self.card_status == false self.card_status = false if self.is_used_up? self.card_status = false if self.is_expired? self.save! end end If I remove all

Query records through its belongs_to relation in Rails

北城余情 提交于 2020-01-28 04:07:55
问题 I have an Activities model, and they belong_to a Location How do i select all the activities whose location.country = Australia? (for example) Can I do this within a scope? 回答1: The kind of query you're talking about is a join. You can try queries like this in the console like: Activity.joins(:locations).where('locations.country = "Australia"') This means that SQL is going to take all the activities and locations associated with then, find the locations where country=Australia, and then

Query records through its belongs_to relation in Rails

谁都会走 提交于 2020-01-28 04:07:06
问题 I have an Activities model, and they belong_to a Location How do i select all the activities whose location.country = Australia? (for example) Can I do this within a scope? 回答1: The kind of query you're talking about is a join. You can try queries like this in the console like: Activity.joins(:locations).where('locations.country = "Australia"') This means that SQL is going to take all the activities and locations associated with then, find the locations where country=Australia, and then

Rails ActiveRecord query to match all params

五迷三道 提交于 2020-01-25 22:05:09
问题 I need to have an ActiveRecord Postgres query that returns results which match all the parameters passed in through an array. Some background: I have a User model, which has many Topics (through Specialties). I'm passing in the Topic ids as a string ( Parameters: {"topics"=>"1,8,3"} ) and then turning them into an array with .split(',') so I end up with topic_params = ["1","8","3"] . Now I'm trying to return all Users who have Topics that match/include all of those. After following the answer