activerecord

return custom query select in activerecord

佐手、 提交于 2019-12-17 19:22:19
问题 I've got a query that does some math and returns a calculated custom select field with the result set. I cannot figure out how to access that in the activerecord object that is returned. I've added an attr_accessor for it also. attr_accessor :percentage_used select('gateways.*, (num_transactions_today/ SUM(num_transactions_today)) AS percentage_used ').joins(:gateway_groups).where('map_gateway_groups.gateway_group_id = ?', gateway_group_id) in the result set, I would expect to have access to

CodeIgniter: INSERT multiple records without cycle

早过忘川 提交于 2019-12-17 18:46:32
问题 It's possible to use multiple INSERT records in CodeIgniter Active Record without for, foreach and etc. ? My current code: foreach($tags as $tag) { $tag = trim($tag); $data = array( 'topic_id' => $topic_id, 'user_id' => $user_id, 'text' => $tag ); $this->db->insert('topic_tags', $data); } 回答1: Codeigniter active record has a function insert_batch i think that is what you need $data = array( array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date' ), array( 'title' => 'Another

Stored procedures in Ruby on Rails

末鹿安然 提交于 2019-12-17 18:27:07
问题 I am a .net guy with 6 years of experience. Recently I started working on ROR project and realized that stored procedures/sql functions are not being used at all. On inquiring about it I got to know that it is common practice and in general nobody in team writes a sql queries at all, everything is done using ActiveRecord. I googled about any possible reasons for this but didn't find much information. So I am just curios to know Is it common practice that stored procedures/sql functions are

using UUID as primary key in rails and polymorph relationships

耗尽温柔 提交于 2019-12-17 18:11:28
问题 I'm creating a rails 3 application that will be decentralized and I need to use UUID as primary key for my tables, what would be the best gem, plugin for the Job. I also would like to know if it is possible to make in ActiveRecord polymorphic relationships without using the polymorphicable_type column for it, given the case that I'm using UUID. I have created a demo http://github.com/boriscy/uuidrails3 that uses UUID as keys, you should check the module UUIDHelper inside lib/ and also all the

can you use activerecord to find substring of a field? (quick & dirty keyword finder)

亡梦爱人 提交于 2019-12-17 18:01:04
问题 Suppose a database contains a field 'keywords' and sample records include: "pipe wrench" "monkey wrench" "crescent wrench" "crescent roll" "monkey bars" is there a way in activerecord to find the records where the keyword field contains the substring "crescent"? (It's just a quick and dirty lookup for a quick concept prototype) 回答1: Yeah, just use a LIKE statement in MySQL. In Rails 2.x: Table.find(:all, :conditions => ['keywords LIKE ?', '%crescent%']) In Rails 3.x: Table.where('keywords

dependent => destroy on a “has_many through” association

本秂侑毒 提交于 2019-12-17 17:38:40
问题 Apparently dependent => destroy is ignored when also using the :through option. So I have this... class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users ... end ...but deleting a Comment does not result in the associated comment_user records getting deleted. What's the recommended approach, then, for cascade deletes when using :through? Thanks 回答1: Apparently :dependent is not ignored! The real issue was that I was

How to implement a unique index on two columns in rails

点点圈 提交于 2019-12-17 17:38:32
问题 I have a table and I'm trying to add a unique index on two columns. Those columns are also indexed. So my question is if I just can remove the indexes who were just for one column or if I have to use all three indexes: add_index "subscriptions", ["user_id"] add_index "subscriptions", ["content_id"] add_index "subscriptions", ["user_id"], ["content_id"], :unique => true 回答1: add_index :subscriptions, [:user_id, :content_id], unique: true 来源: https://stackoverflow.com/questions/4123610/how-to

Rails 3 skip validations and callbacks

大城市里の小女人 提交于 2019-12-17 17:34:59
问题 I have a particularly complex model with validations and callbacks defined. The business needs now calls for a particular scenario where adding a new record requires skipping the validations and callbacks. What's the best way to do this? 回答1: This works in Rails 3: Model.skip_callback(:create) model.save(:validate => false) Model.set_callback(:create) (API docs and related question) 回答2: Use ActiveRecord::Persistence#update_column, like this: Model.update_column(field, value) 回答3: If the goal

has_many :through : How do you access join table attributes?

有些话、适合烂在心里 提交于 2019-12-17 17:34:15
问题 I have the following models in Rails 4 with a simple has_many :through association: class Model < ActiveRecord::Base has_many :model_options has_many :options, through: :model_options end class Option < ActiveRecord::Base has_many :model_options has_many :models, through: :model_options end class ModelOption < ActiveRecord::Base belongs_to :model belongs_to :option end I want to be able to iterate over a Model instance's Options: model = Model.find.first model.options.each {} and access the

Multiple table joins in rails

两盒软妹~` 提交于 2019-12-17 17:31:43
问题 How do I write the mysql query below into rails activerecord select A.*, B.* from raga_contest_applicants_songs AS A join raga_contest_applicants AS B ON B.contest_applicant_id = A.contest_applicant_id join raga_contest_rounds AS C ON C.contest_cat_id = B.contest_cat_id WHERE C.contest_cat_id = contest_cat_id GROUP BY C.contest_cat_id I know how to write joins on two tables; however, I'm not very confident on how to use join on 3 tables. 回答1: To rewrite the SQL query you've got in your