activerecord

yii2 behaviors ActiveRecord::EVENT_BEFORE_INSERT not working

自闭症网瘾萝莉.ら 提交于 2020-01-03 21:48:30
问题 My behavior function in my model is as follows public function behaviors() { return [ 'timestamp' => [ 'class' => 'yii\behaviors\TimestampBehavior', 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['log_in_time' ], ActiveRecord::EVENT_BEFORE_UPDATE => ['log_in_time'], ], 'value' => new Expression('NOW()'), ], ]; } /** * validation rules */ public function rules() { return [ ['username','filter', 'filter' => 'trim'], ['username','required'], //['username','unique'], ['username','string'

running sanitize_sql_array outside of a model

扶醉桌前 提交于 2020-01-03 21:25:10
问题 I'm writing a script to build some sql queries and I want to sanitize the query before I execute it. When i call sanitize_sql_array on the array I hit an exception, 'undefined method 'sanitize_sql_array''. How do utilize that active record function without calling from within a model? 来源: https://stackoverflow.com/questions/30808148/running-sanitize-sql-array-outside-of-a-model

Rails - Using join with custom-named associations

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 21:03:05
问题 I have the following model class Measurement < ApplicationRecord belongs_to :examination, class_name: "TestStructure", foreign_key: "examination_id" end The association is actually made to the TestStructure model, but the association name is examination. There is no examination table. The problem arises when I'm querying using join . The following query Measurement.joins(:examination).where(examination: { year: 2016, month: 5 }) fails, with this error ActiveRecord::StatementInvalid: PG:

__call catches static method calls

五迷三道 提交于 2020-01-03 19:39:53
问题 I'm using both the magic methods _call and _callStatic for my own implementation of something like an ORM/Activerow. They're mainly meant for catching certain function calls: __call is responsible for getters and setters, and __callStatic for findBy methods (e.g. findById ). To map foreign keys, i'm trying to convert calls to e.g. getArticle to return the value of Article::findById() . To do that, i'm using this case inside my __call : if (strstr($property, "_id")) { return $foreignClass:

__call catches static method calls

[亡魂溺海] 提交于 2020-01-03 19:39:11
问题 I'm using both the magic methods _call and _callStatic for my own implementation of something like an ORM/Activerow. They're mainly meant for catching certain function calls: __call is responsible for getters and setters, and __callStatic for findBy methods (e.g. findById ). To map foreign keys, i'm trying to convert calls to e.g. getArticle to return the value of Article::findById() . To do that, i'm using this case inside my __call : if (strstr($property, "_id")) { return $foreignClass:

Combining two different ActiveRecord collections into one

佐手、 提交于 2020-01-03 19:30:32
问题 I want create a visual timeline of all company events. The HTML for the timeline is built using a loop . For simplicity let's assume two models Hire & Deal . Both models have a date attribute and some model specific attributes. How do I merge the ActiveRecord results of both models, and then order the combined hash by date , into a single hash that I can loop through? 回答1: Well, assuming that the result of querying both Hire and Deal models is an array of objects ( collection ), then you just

ActiveRecord: Skip validation when saving multiple objects

无人久伴 提交于 2020-01-03 16:04:13
问题 I know I can skip validations for an individual save, like this: User.new(name: 'John').save(validate: false) But how can I do that when saving multiple objects at once? Like this: Category.create([ { name: 'Apps' }, { name: 'Songs' }, { name: 'Movies' } ]) 回答1: I found this gem: https://github.com/zdennis/activerecord-import It works like this: categories = [ Category.new(name: 'Apps'), Category.new(name: 'Songs'), Category.new(name: 'Movies') ] Category.import(categories, validate: false)

Return type from a ActiveRecord query

余生颓废 提交于 2020-01-03 15:37:22
问题 What will let me know if I am going to get a Relation, Array, or some other type back from an ActiveRecord call? I know I can type .class in the console and figure it out, but is there something in the call itself that will let me know what I am asking for? 回答1: You know, Rails sometimes lies to you -- all magicians do :) Rails allows you to build complex queries by chaining your has_many associations. The core of this functionality is a bunch of XXXAssocation (like HasManyAssociation )

Could not find the association, Rails 3

﹥>﹥吖頭↗ 提交于 2020-01-03 15:17:33
问题 class Membership < ActiveRecord::Base belongs_to :role belongs_to :user end class User < ActiveRecord::Base has_many :roles, :through => :memberships end class Role < ActiveRecord::Base has_many :users, :through => :memberships end and my View <% for role in Role.find(:all) %> <div> <%=check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %> <%=role.name%> </div> <% end %> I've got next error on my View - Could not find the association :memberships in model User and i can't

ActiveRecord - select first record from each group

天大地大妈咪最大 提交于 2020-01-03 13:35:12
问题 I want most recent message sent by each users. Here is sample data Table : conversations sender receiver message date ============================================================ 1 2 "hi" "2013-03-04 09:55:01.122074" 2 1 "hello" "2013-03-04 09:55:32.903975" 1 2 "have you done with the taks?" "2013-03-04 09:57:46.383007" 2 1 "almost..." "2013-03-04 09:58:55.783219" 2 1 "should be able to finish 2day" "2013-03-04 09:59:28.950705" 2 3 "shall we start?" "2013-03-04 10:01:16.842725" 3 2 "give me a