activerecord

Index for multiple columns in ActiveRecord

六眼飞鱼酱① 提交于 2019-12-29 10:18:34
问题 In ActiveRecord there are two ways to declare indexes for multiple columns: add_index :classifications, [:species, :family, :trivial_names] add_index :classifications, :species add_index :classifications, :family add_index :classifications, :trivial_names Is there any difference between the first approach and the second one? If so, when should I use the first and when the second? 回答1: You are comparing a composite index with a set of independent indices. They are just different. Think of it

When to add what indexes in a table in Rails

天涯浪子 提交于 2019-12-29 10:08:09
问题 I have a question about Rails database. Should I add "index" to all the foreign keys like "xxx_id"? Should I add "index" to the automatically created "id" column? Should I add "index(unique)" to the automatically created "id" column? If I add index to two foreign keys at once ( add_index (:users, [:category, :state_id]) , what happens? How is this different from adding the index for each key? class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t

Adding existing has_many records to new record with accepts_nested_attributes_for

删除回忆录丶 提交于 2019-12-29 07:38:11
问题 The error "Couldn't find Item with ID=123 for Payment with ID=" occurs when adding existing Item models to a new Payment model. This is in a has_many relationship and using accepts_nested_attributes_for. class Payment < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items ... class Item < ActiveRecord::Base belongs_to :payment ... The payment and item models are hypothetical but the problem is real. I need to associate the Items with the Payment (as I do in the new action)

does rails postgres adapter support ssl?

試著忘記壹切 提交于 2019-12-29 07:34:10
问题 i'm trying to configure a rails app to remotely connect to a postgres db. i've noticed that the connection adapters for mysql have options that specify the required info for setting up an ssl connection, but there is no equivalent options for the postgres/pg adapter. after googling around, i haven't been able to find anything either (only connecting via an ssh tunnel). so simply, is trying to get the rails postgres adapter to connect over ssl a dead end? thanks. any help or direction is

Higher level database layer for Android?

女生的网名这么多〃 提交于 2019-12-29 03:03:45
问题 Are there any good database abstraction layers/object relational mappers/ActiveRecord implementations/whatever they are called for Android? I'm aware that db4o is officially supported, but it has quite a large footprint and I'd rather use a more conventional database ( SQLite ). 回答1: I am the main author of ORMLite which was designed to be small[ish] but still provide higher level functionality. ORMLite makes calls to the native Android OS database APIs to support its ORM functionality. See

group by + sum on multiple columns in rails 3

纵然是瞬间 提交于 2019-12-29 00:40:30
问题 I need to get a list of locations ordered by the number of picture that I have in the DB for these locations, here is my query Location.select(:city).group(:city).order("SUM(images_count) DESC").sum(:images_count) This worked like a charm, unfortunately, I now need to add province and country to prevent ambiguities to arise, so I now have this Location.select(:city, :province, :country).group(:city, :province, :country).order("SUM(images_count) DESC").sum(:images_count) And this is not

Changing table name at query run time in a Rails application

偶尔善良 提交于 2019-12-28 19:34:31
问题 I have a fat multi-tenant Rails app on Apache + mod_passenger that outputs product prices from a PostgreSQL table as follows: Table "public.products" Column | Type id | bigint name | character varying(100) price | numeric(8,2) Then inside products.rb I have... class Product < PostgresDatabase self.table_name = "products" # ... yadda yadda end What I want is to partition the "products" table in a very specific way so that I end up with something like products_TENANT-ID for each tenant

attr_accessible in rails Active Record

怎甘沉沦 提交于 2019-12-28 15:23:07
问题 When I use the attr_accessible to specify which fields from my Model I will expose, is it true for script/console as well? I mean something that I didn't specify as attr_accessible won't be accessible as well through console ? 回答1: This is only true for mass assignment. For instance, if you were to set attr_protected :protected in your model: >> Person.new(:protected => "test") => #<Person protected: nil> Conversely, you could set all attributes you want as accessible using attr_accessible .

attr_accessible in rails Active Record

冷暖自知 提交于 2019-12-28 15:21:17
问题 When I use the attr_accessible to specify which fields from my Model I will expose, is it true for script/console as well? I mean something that I didn't specify as attr_accessible won't be accessible as well through console ? 回答1: This is only true for mass assignment. For instance, if you were to set attr_protected :protected in your model: >> Person.new(:protected => "test") => #<Person protected: nil> Conversely, you could set all attributes you want as accessible using attr_accessible .

Codeigniter's `where` and `or_where`

僤鯓⒐⒋嵵緔 提交于 2019-12-28 13:20:11
问题 I'm trying to specify a query in my model $this->db ->select('*') ->from('library') ->where('library.rating >=', $form['slider']) ->where('library.votes >=', '1000') ->where('library.language !=', 'German') ->where('library.available_until >=', date("Y-m-d H:i:s")) ->or_where('library.available_until =', "00-00-00 00:00:00") ->where('library.release_year >=', $year_start) ->where('library.release_year <=', $year_end) ->join('rating_repo', 'library.id = rating_repo.id') So, the trouble i'm