activerecord

Select * sql query vs Select specific columns sql query [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:58:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why is SELECT * considered harmful? Probably a database nOOb question. Our application has a table like the following TABLE WF Field | Type | Null | Key | Default | Extra | +--------------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | children | text | YES | | NULL | | | w_id | int(11) | YES | | NULL | | | f_id | int(11) | YES | | NULL | | |

Ignoring certain field when querying in Rails and ActiveRecord

ⅰ亾dé卋堺 提交于 2019-12-23 09:40:36
问题 I know that I can specify certain fields to query the database with pluck . ids = Item.where('due_at < ?', 2.days.from_now).pluck(:id) However I would like to know, whether there is a way to specify certain fields I would like to avoid querying from the db. Some kind of anti-pluck? posts = Post.where(published: true).do_not_lookup(:enormous_field) 回答1: Model#attribute_names should return array of columns/attributes. You can exclude some of them and pass to pluck or select methods. Something

Rails paginate existing array of ActiveRecord results

我与影子孤独终老i 提交于 2019-12-23 09:37:30
问题 I generally use will_paginate for the pagination in my app, but have hit a stumbler on my search feature. I'm using Thinking Sphinx for doing my full-text search, which returns results paginated. The problem I'm having is that after I've received the results from Thinking Sphinx, I need to merge them with some other results and re-order them. Once I've finished processing them I have an Array of results that is very different from the original from TS. As there could be 1000+ results in this

Rails - See generated SQL queries in Log files

旧时模样 提交于 2019-12-23 09:36:36
问题 Is there a way to see all generated queries from rails in production environment like you can in development? 回答1: Yes you can. If you go in the config/environments/production.rb file, there's a section like this: # See everything in the log (default is :info) # config.log_level = :debug Uncomment the config.log_level line, and you'll get the same log in production as you would in dev. 来源: https://stackoverflow.com/questions/3852832/rails-see-generated-sql-queries-in-log-files

ActiveRecord: Pass class instead of string to class_name when defining associations

拟墨画扇 提交于 2019-12-23 09:34:48
问题 Are there any implications or gotchas to passing in a class instead of a string when defining an association? belongs_to :owner, class_name: User As opposed to: belongs_to :owner, class_name: "User" 回答1: The class may not be loaded yet in which case you'll get a NameError: uninitialized constant User . You're supposed to use "User" for this reason, as implied by the option name: :class_name , not :class . 回答2: In rare case I encounter some random errors when using classes (User) instead of

Rails ActiveRecord: Is a combined :include and :conditions query possible?

天涯浪子 提交于 2019-12-23 09:31:35
问题 Imagine I have wiki Articles, with many Revisions. I'd like to do a query with ActiveRecord through the database, which only returns those Articles which have Revisions which are updated in the past 24 hours. Is such a thing possible? I'd imagine it'd be something like: Articles.find_all(:include => :revisions, :conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc}) If this isn't possible, is there some type of raw SQL I could pass to find_by_SQL that'd do the trick? 回答1: If you

Manually updating attributes mounted by Carrierwave Uploader

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 09:27:52
问题 I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_uploader statement from the model class it works as normal. I am troubleshooting things from the console and trying to add some attributes while seeding the DB and this is thwarting my efforts. Ideas? Thanks. Update: Relevant code: class Profile < ActiveRecord::Base belongs_to :user has_and_belongs

Return duplicate records (activerecord, postgres)

你。 提交于 2019-12-23 09:21:13
问题 I have the following query returning duplicate titles, but :id is nil : Movie.select(:title).group(:title).having("count(*) > 1") [#<Movie:0x007f81f7111c20 id: nil, title: "Fargo">, #<Movie:0x007f81f7111ab8 id: nil, title: "Children of Men">, #<Movie:0x007f81f7111950 id: nil, title: "The Martian">, #<Movie:0x007f81f71117e8 id: nil, title: "Gravity">] I tried adding :id to the select and group but it returns an empty array. How can I return the whole movie record, not just the titles? 回答1: A

Restrict the columns represented in ActiveRecord

坚强是说给别人听的谎言 提交于 2019-12-23 08:28:14
问题 How to change ActiveRecord so that it always has a restricted set of columns. I dont want all the columns in the backened table to present in the Model. This unnecessarily bloats the ActiveRecord's memory footprint as well as the time taken to query the record. There are attributes like select (ar.rubyonrails.org/classes/ActiveRecord/Base) which can be used to SELECT only few columns. But is there any way we can force ActiveRecord to never query those columns inspite of the user performing

Codeigniter deleting data with joins tables

我是研究僧i 提交于 2019-12-23 07:46:32
问题 Logically in SQL we can delete data from tables with JOINS e.g. DELETE clb_subscriber_group_mapping .* FROM clb_subscriber_group_mapping INNER JOIN clb_driver_group ON (clb_driver_group.id = clb_subscriber_group_mapping.driver_group_id) INNER JOIN clb_company ON (clb_company.id = clb_driver_group.company_id) WHERE clb_company.id = 256 AND clb_subscriber_group_mapping.subscriber_id = 1784; What will be the CodeIgniter equivalent of above query? Does CodeIgniter support Delete query with joins?