activerecord

PHP ActiveRecord: How do I add multiple conditions?

北战南征 提交于 2019-12-08 08:02:02
问题 I'm trying to bind more than one condition in php ActiveRecord How can I change the below code (which works) to bind another condition? $events = Event::find('all', array( 'conditions' => array('event_type = ?', array('opera')), 'select' => 'col1, col2, col3', )); Thanks! 回答1: Try this $events = Event::find('all', array( 'conditions' => array('event_type = ? AND event_hour = ?', 'opera', 'your_event_hour'), 'select' => 'col1, col2, col3', )); 回答2: $objectChanges = Table_model::update_all

Fetch a Rails ActiveRecord 'datetime' attribute as a DateTime object

∥☆過路亽.° 提交于 2019-12-08 07:52:59
问题 I have an attribute in one of my models that contains a Date/Time value, and is declared using t.datetime :ended_on in my migrations. When I fetch this value using myevent.ended_on , I get a Time object. The problem is that when I try to use this attribute as an axis in a Flotilla chart, it doesn't work properly because Flotilla only recognizes dates as Date or DateTime objects. I thought about writing a virtual attribute that will convert the existing Time value to a DateTime, but I'm wary

Using Datamapper with existing rails application

牧云@^-^@ 提交于 2019-12-08 07:24:26
问题 I have an existing Rails 3 application using ActiveRecord, and I want to switch to Datamapper. The instructions given in the dm-rails page only talk about creating a new application. Does anyone know how to throw away all activerecord dependancies and migrate to datamapper? Thanks! 回答1: It's realtively straightforward, but there are a couple of things you need to do. In your Gemfile, remove "rails" and instead require the following. gem 'activesupport', RAILS_VERSION, :require => 'active

Rails group a table by :created_at, returning a count of the :status column, and then sub-group the :status column with count of each unique value

无人久伴 提交于 2019-12-08 07:15:28
I have a model Notification and I would like to know how many notifications are created per days and how many with each status. status is a string key of the model Notification. Notification.where(user: users).group('DATE(created_at)').count('created_at') That give me : {Mon, 05 Oct 2015=>2572, Tue, 06 Oct 2015=>555, Wed, 07 Oct 2015=>2124, Thu, 08 Oct 2015=>220, Fri, 09 Oct 2015=>36} But I would like something like : {Mon, 05 Oct 2015=>{total=>2572, error=>500, pending=>12}, Tue, 06 Oct 2015=>{total=>555, sent=>50, pending=>12}} Or similar. Is it possible with active record ? Maybe group_by

Get max bookings count in range

廉价感情. 提交于 2019-12-08 07:05:06
问题 I have a ParkingLot model. Parking Lots have a number of available lots . Users can then book a parking lot for one or more days. Hence I have a Booking model. class ParkingLot has_many :bookings end class Booking belongs_to :parking_lot end Simplified Usecase ParkingLot Given a parking lot with 5 available lots: Bookings Bob books a place from Monday to Sunday Sue makes one booking each on Monday, Wednesday and Friday Henry books only on Friday. Since the weekend is busy, 4 other people book

Active record query failed - Escape quote from query

大憨熊 提交于 2019-12-08 06:56:26
问题 Background Framework: Codeignighter/PyroCMS I have a DB that stores a list of products, I have a duplicate function in my application that first looks for the common product name so it can add a ' suffix ' value to the duplicated product. Code in my Products model class $product = $this->get($id); $count = $this->db->like('name', $product->name)->get('products')->num_rows(); $new_product->name = $product->name . ' - ' . $count; On the second line the application fails only when the $product-

Existing Rails model without fetching it from the database

孤者浪人 提交于 2019-12-08 06:40:27
Does anyone know if its possible to create a model instance and apply the ID and any other attributes without having to load it from the database? I tried doing this, but the associations are not fetched from the database :( Any ideas? EDIT What I want to accomplish is simply this: Fetch an existing record from the database. Store as "hashed" output of the record into redis or some other memory store. Next time when that record is fetched, fetch the cached store first and if it is not found then goto step 1. If there is a cache hit, then load all the cached attributes into that model and make

How to efficiently work with multiple database tables in Ruby on Rails

余生长醉 提交于 2019-12-08 06:38:13
问题 I've giving myself a small project to help me improve with my ruby on rails programming. So far I've completed full user signup and authentication with password reset, a remember me feature and notification emails. I totally understand what is going on. So I've now moved to the user account area and would like to code the page that my user would edit their profile on. My signup form only asked for username, email and password as I wanted quick registration. For the page where users will edit

Overriding instance method in Rails [duplicate]

痴心易碎 提交于 2019-12-08 06:32:01
问题 This question already has answers here : Why do Ruby setters need “self.” qualification within the class? (3 answers) Closed 5 years ago . While overriding some assignment methods, I discovered that if I use the implicit receiver in the field_two method, the first overridden method, field_one, doesn't get called. Instead, the default 'field_one=' seems to be called. #controller def do_something @something=Something.first @something.field_two="some_value" end class Something<ActiveRecord::Base

Make sure has_many :through association is unique on creation

允我心安 提交于 2019-12-08 06:26:01
问题 If you are saving a has_many :through association at record creation time, how can you make sure the association has unique objects. Unique is defined by a custom set of attributes. Considering: class User < ActiveRecord::Base has_many :user_roles has_many :roles, through: :user_roles before_validation :ensure_unique_roles private def ensure_unique_roles # I thought the following would work: self.roles = self.roles.to_a.uniq{|r| "#{r.project_id}-#{r.role_id}" } # but the above results in