activerecord

Yii2: validation errors that are not attribute specific

可紊 提交于 2020-01-07 08:28:12
问题 How do I handle generic validation errors in a Yii2 ActiveRecord model that do not relate to a specific attribute ? For example I need to completely prohibit saving a model when a related record/modal has been set to inactive. I can of course just pick a more or less random attribute and assign the error message to it, but what if the front end form doesn't have that attribute and therefore doesn't show the error? Or even worse, if a scenario later disables validation of that attribute (by

has_many through with condition not working

爱⌒轻易说出口 提交于 2020-01-07 06:28:09
问题 I have a model Grade and a model User. Between grades and user is a many-to-many association through collaborations. in user.rb has_many :grades, through: :collaborations, source: :user works, but i need to get only grades with attribute "archived" = false i tryied has_many :grades, through: :collaborations, source: :user, conditions: [' archived = ? ', false] but it takes all the grades, in other words, the condition is ignored. I can put in my collaborations this condition, but

Transformation of query to active record

安稳与你 提交于 2020-01-07 05:34:08
问题 To perform a range query we follow something akin to the syntax below -: oms[:order_items].where(:internal_sla => 3..5) results in this query => #<Sequel::Mysql2::Dataset: "SELECT * FROM `order_items` WHERE ((`internal_sla` >= 3) AND (`internal_sla` <= 5))"> But how can I change the active record query to give me something like this => select internal_sla from order_items where (internal_sla<=3 and internal_sla>=0) OR (internal_sla<=15 and internal_sla>=10) 回答1: .where("(internal_sla >= ? AND

Find entry where value does not intersect with other value

一世执手 提交于 2020-01-07 03:51:24
问题 I am having issues making an ActiveRecord query in Ruby on Rails that gives me back all users, whose blocked_dates (autogenerated string value) do not intersect with the formatted_dates (autogenerated string value) of a given event. The problem I'm having is that, for example: User.where.not("string_to_array(blocked_dates, ',') && string_to_array(?, ',')", "26.12.2015") Gives back an empty list, whereby: User.where("string_to_array(blocked_dates, ',') && string_to_array(?, ',')", "26.12.2015"

How to validate unique id's in two columns in rails?

给你一囗甜甜゛ 提交于 2020-01-07 03:07:05
问题 I know that to prevent this from happening: user_id: 10 | friend_id: 20 user_id: 10 | friend_id: 20 You can use validates :user_id, uniqueness: {scope: :friend_id} But what if you want to prevent a user from adding himself as a his own friend? user_id: 10 | friend_id: 10 回答1: This should work. You're ensuring that friend_id can't equal user_id validates :friend_id, exclusion: {in: ->(u) { [u.user_id] }} 来源: https://stackoverflow.com/questions/33698881/how-to-validate-unique-ids-in-two-columns

How to validate unique id's in two columns in rails?

非 Y 不嫁゛ 提交于 2020-01-07 03:06:10
问题 I know that to prevent this from happening: user_id: 10 | friend_id: 20 user_id: 10 | friend_id: 20 You can use validates :user_id, uniqueness: {scope: :friend_id} But what if you want to prevent a user from adding himself as a his own friend? user_id: 10 | friend_id: 10 回答1: This should work. You're ensuring that friend_id can't equal user_id validates :friend_id, exclusion: {in: ->(u) { [u.user_id] }} 来源: https://stackoverflow.com/questions/33698881/how-to-validate-unique-ids-in-two-columns

Use Yii findAll to return a model w/ all properties

為{幸葍}努か 提交于 2020-01-07 02:24:40
问题 I am still new to Yii and wondering how to return JSON from the $models = MyModel::model()->findAll(); . Say for example MyModel has a relation for MyChildModels in a ONE:MANY fashion. Straight from the Rest example on the Yii site I have: foreach ($models as $model) { $rows[] = $model->attributes; } $this->_sendResponse(200, CJSON::encode($rows), 'application/json'); I get all of the model's attributes but NOT the joined relation attributes. Similarly, I can change the $rows line to be:

How to optimize this query in ActiveRecord - latest 10 topics by friends

岁酱吖の 提交于 2020-01-06 20:01:35
问题 User model: user_id phone_no ------- -------- 1 555-0001 2 555-0002 . . Every user has his/her own phone number. Friend model: user_id phone_no ------- -------- user 1 555-0002 user 1 555-0003 user 2 555-0001 user 2 555-0004 User may has many phones numbers. A phone number match is consider as a friend (one direction), e.g. owner of 555-0003 is a friend of user 1 , not vise versa. Topic model: topic_id user_id title -------- ------- ----- 1 1 Hello 2 1 Hi How to optimize the query of getting

Yii2 updating two related models does not show data of the second

妖精的绣舞 提交于 2020-01-06 19:55:34
问题 I have two related models in hasToMany relation. The first is Invoices and the second is InvoiceItems . As a prototype process, I'm trying to conjugate the two models through the update view of the InvoicesController using the following code in actionUpdate of InvoicesController : ... use common\models\Invoices; use common\models\InvoicesSearch; use common\models\InvoiceItems; ... public function actionUpdate($id) { $model = $this->findModel($id); $invoiceItems = new InvoiceItems(); if (

undefined method 'info' for nil:NilClass when running active record migration

人走茶凉 提交于 2020-01-06 19:46:44
问题 I am trying to run an active record migration but am receiving the following error: undefined method 'info' for nil:NilClass Here is the 2 lines of code in my rake task that runs the migration ActiveRecord::Base.establish_connection(YAML::load(File.open('src/SupporterSync.Core/Database/Database.yml'))) ActiveRecord::Migrator.migrate('src/SupporterSync.Core/Database/Migrations', ENV["VERSION"] ? ENV["VERSION"].to_i : nil ) And here is my only migration class in the folder class