activerecord

how to update attributes in self model rails

谁都会走 提交于 2019-12-07 17:21:27
问题 how to update self model in rails? class BillSubTotal < ActiveRecord::Base def self.total self.update_attributes(:total => 150) end end i want update value in model BillSubTotal from self model? 回答1: Depends on what you're wanting to do as how you'd go about this. Rails automatically creates the getter/setter methods for database columns, so you don't have to create that functionality explicitly, and can just set the method like so: class BillSubTotal < ActiveRecord::Base def update_total

Newly assigned Sequence is not working

ⅰ亾dé卋堺 提交于 2019-12-07 16:38:10
问题 In PostgreSQL, I created a new table and assigned a new sequence to the id column. If I insert a record from the PostgreSQL console it works but when I try to import a record from from Rails, it raises an exception that it is unable to find the associated sequence. Here is the table: \d+ user_messages; Table "public.user_messages" Column | Type | Modifiers | Storage | Description -------------+-----------------------------+------------------------------------------------------------+---------

Rails updating multiple models on a single form

时光总嘲笑我的痴心妄想 提交于 2019-12-07 16:36:18
问题 Im writing a form which uses formtastic to manage the BusinessUnit model, however when creating a new BusinessUnit it also has to create a number of other record types. The associations between the models are as below: class BusinessUnit < ActiveRecord::Base has_many :business_unit_sites has_many :locations class BusinessUnitSite < ActiveRecord::Base belongs_to :site belongs_to :business_unit class Site < ActiveRecord::Base has_many :locations has_many :business_unit_sites class Location <

How to call ActiveRecord validators as instance methods (ala Sequel)?

℡╲_俬逩灬. 提交于 2019-12-07 16:12:26
问题 I've got a model that needs different validators depending on its current state. How should I go about calling ActiveRecord validators per instance? I'd like to reuse as much plumbing as possible, but I'm not sure how to continue. class Order < ActiveRecord::Base attr_accessible :state validate :state_specific_validations def state_specific_validations if :paid == self.state # Warning: here be Unicorns... # Wishful thinking... validate_presence_of :paid_at validate_associated :purchaser #

How can I compare a BigDecimal with ActiveRecord decimal field?

浪尽此生 提交于 2019-12-07 16:07:04
问题 Assume a schema like this: create_table "bills", :force => true do |t| t.decimal "cost", :precision => 10, :scale => 5 end I want to write a function that writes a new bill to the DB iff it is unique. The following does not work: def load_bill_unless_exists(candidate) incumbents = Bill.scoped.where(:cost => candidate.cost) candidate.save unless incumbents.exists? end because the incumbent bills and the candidate bills have different limits in their BigDecimal representation, so the :cost =>

Is it possible to group by hour/minute/quarter hour directly in ActiveRecord/Rails?

眉间皱痕 提交于 2019-12-07 15:37:32
问题 I want to group my data by the hour/day possibly even to the quarter hour. Can I do this directly in ActiveRecord or should I just grab all the data and do the aggregation manually? Table "Checkin": Time NumBikes ChangeBikes 09:00 5 5 09:05 6 1 09:10 10 4 10:00 6 4 10:05 8 2 10:10 16 8 Looking to get something like: Time Sum(ChangeBikes) 09:00 10 10:00 14 Thanks in advance, Chris. 回答1: In case you do not need the quarter hour or as an alternative to this (with some work) (and Chris may have

NHibernate / ActiveRecord: How to set foreign key without getting entire object?

孤街醉人 提交于 2019-12-07 15:14:04
问题 Let's say I've got the following ActiveRecord class: [ActiveRecord] public class Account { ... [BelongsTo("CustomerId")] public Customer Customer { get; set; } } Currently, to set the value of the CustomerId field I have to get the entire Customer object from the database and assign it to the Account: Customer customer = Customer.FindById(1); account.Customer = customer; This isn't very efficient. I'd rather set the value of CustomerId field directly without round-tripping the database, e.g.

Yii2: not able to update column value by + 1

巧了我就是萌 提交于 2019-12-07 14:44:47
问题 I need to update column value by + 1 when new records get created : public function actionCreate() { $model = new CreateBookings(); if ($model->load(Yii::$app->request->post())) { Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type' ")->execute(); $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } } What am I doing wrong please guide :) 回答1

Rails ActiveRecord - is there a way to perform operations on tables without an id?

核能气质少年 提交于 2019-12-07 14:35:29
问题 I have a table with one row and two columns- int 'version', datetime 'updated' Is there a Rails ActiveRecord way to get and set the data in these columns? There is no id column. I'm using this table to track versions of queries of other tables. After each query of another table the version column is incremented and the updated column is set with current datetime. 回答1: Nothing prevent you to use it in ActiveRecord without ID : The migration contains : create_table :posts, :id => false do |t| t

Implementing an ActiveRecord before_find

冷暖自知 提交于 2019-12-07 14:29:55
问题 I am building a search with the keywords cached in a table. Before a user-inputted keyword is looked up in the table, it is normalized. For example, some punctuation like '-' is removed and the casing is standardized. The normalized keyword is then used to find fetch the search results. I am currently handling the normalization in the controller with a before_filter. I was wondering if there was a way to do this in the model instead. Something conceptually like a "before_find" callback would