activerecord

Codeigniter, increase database value by value contained in variable

廉价感情. 提交于 2021-02-02 09:13:16
问题 I am using codeigniter, and I have the following function in my model to give points to the user. It is however not working, rather setting the points column to 0. This is how it is written in the codeigniter manual. Therefore I have no clue why it is not working... Thanks function give_points($username,$points) { $this->db->set('points', 'points + $points'); $this->db->where('username', $username); $this->db->update('users'); echo"done"; } 回答1: I believe you have to tell CI specifically to

Is it possible to define a scope for a non db column

拥有回忆 提交于 2021-01-29 12:51:44
问题 We have a method in our model occurs_on? Enrollment.first.occurs_on?(Date.current) which returns true if an enrollment falls on a Date (and false if not). Enrollments have a recurring schedule and it's not possible to do a db query for occurs_on? without doing calculations in Ruby. It's all working fine but this is throwing off our pagination. Is it possible to define a scope that doesn't depend on a database column? For example something like scope :occurs_today, -> { occurs_on?(Date.current

Is it possible to define a scope for a non db column

╄→гoц情女王★ 提交于 2021-01-29 11:50:25
问题 We have a method in our model occurs_on? Enrollment.first.occurs_on?(Date.current) which returns true if an enrollment falls on a Date (and false if not). Enrollments have a recurring schedule and it's not possible to do a db query for occurs_on? without doing calculations in Ruby. It's all working fine but this is throwing off our pagination. Is it possible to define a scope that doesn't depend on a database column? For example something like scope :occurs_today, -> { occurs_on?(Date.current

Ruby on Rails. How to show record only once?

柔情痞子 提交于 2021-01-29 04:20:54
问题 I want to know how to show a record only exactly after it was created. So i want to show it only once. Just see. I have model called Claim. User creates new Claim (user is anonymous). I want to show this Claim only ofter user creates it and never again. How can I do it? I think that I can use flash hash, but i think it is not enough secure to create symbols from user data. 回答1: You could have a viewed attribute on your model that you set to true in the show action. If viewed is true then skip

Case Statements in ARel

孤者浪人 提交于 2021-01-28 19:06:45
问题 I'm trying to clean up an ActiveRecord code written with MySQL case statements using ARel. I know Arel is able to handle case statements, but not sure how to use it. So I have an orders table with quantity and user_id(foreign key) columns. I'm dynamically computing the price of orders as there are different pricing schemes based on the quantity ordered. This is what the AR code looks like: def orders_with_price <<-SQL orders.*, SUM(CASE orders.quantity WHEN 2 THEN 500 * orders.quantity WHEN 5

Rails4 ActiveRecord has_one :conditions key removed

我与影子孤独终老i 提交于 2021-01-28 12:10:40
问题 I am converting a RoR3.2 app to v4.2. In some models we have this construct: # A correspondent can have only one currently active client role or # it may have none. has_one :active_client, :class_name => 'Client', :conditions => IsActiveRow I am getting this error: Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :dependent, :primary_key, :inverse_of, :required, :as, :foreign_type (ActionView::Template::Error) Which is raised here: /home

CodeIgniter - How to SELECT all rows in a big table without memory leake

对着背影说爱祢 提交于 2021-01-28 06:15:55
问题 It's kinda hard to undertsand my need in the title. CodeIgniter is performing a SELECT query in a table of 800,000+ rows in one shot. It takes a lot of memory, but in one specific server, I get a "Out of memory" fatal error. For performance purposes, I would like to seperate the select into 2 selects, and more specifically, the 50% first rows, and then the 50% left. I reuse this set of data to perform an INSERT afterwise. How to do that without losing/forgetting any single row ? 回答1: Beside

Not Equal to Comparison in Codeigniter Active records

久未见 提交于 2021-01-28 05:03:45
问题 I am quite confused with this error which i am receiving with this code $this->db->order_by('uid','DESC')->where('type!=',"Admin")->get('user_profile',$config['per_page'], $this->uri->segment(3)); Here is what I get the Error Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Admin' ORDER BY `uid` DESC LIMIT 10' at line 3 SELECT * FROM (`user_profile`) WHERE `type!=` 'Admin' ORDER BY `uid`

Many-to-many relation in yii2 activedataprovider

三世轮回 提交于 2021-01-28 05:01:07
问题 I have many-to-many relation with three tables: Category , Product , ProductCategory . Relation in Category : public function getProductCategories() { return $this->hasMany(ProductCategory::className(), ['category_id' => 'id']); } Relation in Product : public function getProductCategories() { return $this->hasMany(ProductCategory::ClassName(), ['product_id' => 'id']); } And in ProductCategory public function getProduct() { return $this->hasOne(Product::className(), ['id' => 'product_id']); }

How can I implement dynamic validation in activerecord

谁都会走 提交于 2021-01-28 01:44:52
问题 What is the best way to adjust your validation of a model based on a parameter or action? Say I am entering a lead into a system, so all that is required is basic contact info. But then later I may enter a new contract for that user at which point I need more detailed information. So I may need phone number when just entering a lead but once they are entering into a contract I might need their birthdate and alternate phone number. I considered using a state machine but the user could actually