activerecord

CodeIgniter - Grouping where clause

只谈情不闲聊 提交于 2019-12-11 03:59:55
问题 I have this following query for CodeIgniter: $q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")') ->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")') ->get('messages'); I want to write this query with completely active record. I have tried something like this: $from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']); $to_where = array(

Rails ActiveRecord and DB normalization

孤者浪人 提交于 2019-12-11 03:57:25
问题 What are the pros and cons of breaking out optional 1:1 attributes into their own separate model. For example, I just encountered Rails code like this: class Dogs << ActiveRecord::Base # :id (pk), :breed, :weight, :height, :tail_length end class DogSpotsInfo << ActiveRecord::Base # :dog_id (pk), :spot_color, :avg_spot_size, :num_spots end But this is how I would have done it (leaving the spot fields null as necessary): class Dogs << ActiveRecord::Base # :id, :breed, :weight, :height, :tail

Pluck last value of an association in rails

房东的猫 提交于 2019-12-11 03:53:11
问题 The model Price has an attribute data, so Price.pluck(:data) results in something like [3.99,4.55,5.44] And Price belongs to Vendor So I want to select the best price for each vendor, something like Vendor.all.pluck(prices.order("data ASC").first.data) How can I go about extracting the lowest price data element for each vendor in this scenario? Thank you in advance for the help. 回答1: For each vendor, use minimum(:data) on its associated prices: Vendor.includes(:prices).map { |v| v.prices

issue in order clause and limit in active record

柔情痞子 提交于 2019-12-11 03:46:59
问题 I am triggering this query p "my query starts here..." @relevant_customers_for_total_spent = Customer.order("total_amount DESC").where('id IN (?)',@customers_ids).limit(relevant_customers_count_for_total_spent) p " ------- " p relevant_customers_count_for_total_spent # output is: 1139 p @relevant_customers_for_total_spent.count # output is: 2888 p " ------- " More over log is saying the actual query fired is: SELECT COUNT(*) FROM `customers` WHERE (id IN (2,3,4,5,6,75,56,57,58,59,60,61,62,63

Rails/ActiveRecord, how to prevent pluck from overriding select?

走远了吗. 提交于 2019-12-11 03:46:23
问题 As an example, I want to run a query like people = Person.select("GROUP_CONCAT(`first` SEPARATOR ', ') as names") \ .where(last: "smith").group(:age).order(:age) # which basically gives me something like # SELECT GROUP_CONCAT(`first` SEPARATOR ', ') as names ... ##################### # but when I add pluck people.pluck(:names) # it does # SELECT names ... # and gives me an Unknown column error How do I make rails/activerecord to handle pluck on the result of the select query rather than

Same Model with different columns Rails

微笑、不失礼 提交于 2019-12-11 03:43:04
问题 I have 2 user roles Developer and Driver . They both are under an User model, but also both have different details such as Developer has hourly_rate , skills , experience , full_name and Driver has cars_he_can_drive , hours_driven , full_name etc. They have some common columns and some different ones as well. Should there be a separate detail table ( develop_details , driver_details ) for each of the User? and further, relationships can be made with them. Else I can have same model with all

Default conditions for Rails models

青春壹個敷衍的年華 提交于 2019-12-11 03:38:57
问题 I have a model which has a field called deleted , which is used to mark those deleted items. So normally I would just want to query those having deleted = false items, and in some special cases to list those deleted items for restoring. Is it possible to do that? What I could do now is just using a named scope having :conditions => {:deleted => false} Is there a better way to do it so that When I do Item.other_named_scope , I could find all those not-deleted items? 回答1: You can use default

Cancel current/active query in Ruby on Rails

百般思念 提交于 2019-12-11 03:32:40
问题 In my project I am running a massive query returning lots of data. Due to the size of this query, the runtime sometimes exceeds 1 minute. Ignoring other issues this may create, is there any way to cancel a currently running query? For example, the user starts the query and is redirected to a standard 'Loading Results...' page. If they then decide that they no longer want the results, they can click a 'Cancel' button which will somehow terminate the running query. Is this possible and how can

Overriding jOOQ's exception handling for UpdatableRecords

大憨熊 提交于 2019-12-11 03:32:16
问题 I'm using jOOQ v2.6 as I'm using SQL Server 2008 R2 and there is a bug in jOOQ v3.1 which causes code generation to fail. (I'm aware this will be fixed in v3.2). From the manual: // Create a new record BookRecord book1 = create.newRecord(BOOK); // Insert the record: INSERT INTO BOOK (TITLE) VALUES ('1984'); book1.setTitle("1984"); book1.store(); If store() fails, a DataAccessException is thrown. In my case I would simply like the process to sleep until either the CRUD operation works, or I

How do I find records based on attributes of a many-to-many-related model?

邮差的信 提交于 2019-12-11 03:26:18
问题 Models... InternalUser has_many :internal_user_roles has_many :roles, :through => :internal_user_roles InternalUserRole belongs_to :internal_user belongs_to :role Role has_many :internal_user_roles has_many :internal_users, :through => :internal_user_roles Using the new ActiveRecord query API, how would I find all the InternalUser s with the "ADMIN" role? In other words, how do I generate this query... SELECT * FROM internal_users i, internal_user_roles ir, roles r WHERE i.id = ir.internal