activerecord

Weird backticks behaviour in Active Record in CodeIgniter 2.0.3

折月煮酒 提交于 2019-12-18 11:45:16
问题 Previously my all queries were running fine in CI version 2.0 but when I upgraded to 2.0.3 some of my SELECT queries were broken. CI is adding backticks (``) automatically, but in older version its running as it is. CI user manual have instructed to add second parameter in db->select as FALSE but still it's not working. Code is as following: class Company_model extends MY_Model { ---------------- $this->db->select(' count('.$fieldname. ') as num_stations'); $this->db->select(" CONCAT_WS(',',

Correct setting of database connection pool database.yml for single-threaded Rails applications

China☆狼群 提交于 2019-12-18 11:41:26
问题 I was wondering about the following setting in the Rails database.yml: By default the number of database connections for the connection pool of ActiveRecord is set to 5: development: ... pool: 5 But by default, Rails 3 is single threaded. Why would you need 5 connections by default? As far as I understand, a single threaded Rails app can't trigger multiple database operations at once, why would do you need to keep more connection open? I would assume that 2 connections would make sense, so

Ruby on Rails: loading seed data from a YAML file

你离开我真会死。 提交于 2019-12-18 11:32:16
问题 How do I use a YAML file instead of seeds.rb to load the initial data into a database? 回答1: Check out the Ruby on Rails Guide to fixtures: http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures Generally, you can create YAML fixture files in the test/ directory and then load them into your database using the rake db:fixtures:load command. The full documentation on all the cool things you can do with fixtures is here: http://api.rubyonrails.org/classes/Fixtures.html 回答2: Add code

belongs_to with :class_name option fails

谁都会走 提交于 2019-12-18 10:33:31
问题 I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot! Here is a snip from my code. class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.text :name end end def self.down drop_table :users end end ##################################################### class CreateBooks < ActiveRecord::Migration def self.up create_table :books do |t| t.text :title t.integer :author_id, :null => false end

How to get activerecord associations via reflection

家住魔仙堡 提交于 2019-12-18 10:08:59
问题 For normal columns, you can get at them via the columns class method. However, associations may be named something quite different if the foreign_key option is set in the relationship method. For example, given class Post has_many :comments, :foreign_key => :message_id # this is a contrived example end if I did Post.column_names I could get at message_id , but is there any way to get comments ? 回答1: Model.reflections gives information about a model's associations. It is a Hash keyed on the

Rails: has_many through with polymorphic association - will this work?

夙愿已清 提交于 2019-12-18 10:06:30
问题 A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record? Here are the models I have: class Event < ActiveRecord::Base belongs_to :person belongs_to :eventable, :polymorphic => true end class Meal < ActiveRecord::Base has_one :event, :as => eventable end class Workout < ActiveRecord::Base has_one :event, :as => eventable end The main question concerns the Person class: class Person <

Unable to join self-joins tables in Rails

六眼飞鱼酱① 提交于 2019-12-18 09:39:23
问题 I have 2 models class Category < ActiveRecord::Base belongs_to :parent, :class_name => "Category" has_many :children, :class_name => "Category", :foreign_key => "parent_id" has_many :products attr_accessible :description, :title, :parent end class Product < ActiveRecord::Base belongs_to :category end In particular, Category has a parent item titled "tea" and this item has many children items: "black tea", "white tea"... I need to select products that belong to a parent category "tea". Here is

dynamic_matchers.rb:55:in `method_missing': undefined method `migration_error=' for ActiveRecord::Base:Class (NoMethodError)

大城市里の小女人 提交于 2019-12-18 05:18:08
问题 I'm on Windows. Ruby v.1.9.3p392 / Rails v. 3.2.13 - This is the demo_app project from the 2nd chapter of Michael Hart's Ruby on Rails Tutorial. This error comes up when I issue ' rails generate scaffold User name:string email:string ' Any idea how to fix this? C:\ruby\rails_projects\demo_app>rails generate scaffold User name:string email:s tring invoke active_record C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/act ive_record/dynamic_matchers.rb:55:in `method

atomic insert or increment in ActiveRecord/Rails

三世轮回 提交于 2019-12-18 04:48:10
问题 What's the best way to implement an atomic insert/update for a model with a counter in Rails? A good analogy for the problem I'm trying to solve is a "like" counter with two fields: url : string count : integer Upon insert, if there is not currently a record with a matching url, a new record should be created with count 1; else the existing record's count field should be incremented. Initially I tried code like the following: Like.find_or_create_by_url("http://example.com").increment!(:count)

Integer out of range in PostgreSQL database

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 04:35:17
问题 I'm trying to save a number representing the length of a file (4825733517). The column is set to type integer. I don't have any validations or restrictions set. RangeError: 4825733517 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 4 Should I be using some other column type for this value? (on rails 4.2.4) 回答1: For columns of type integer , the :limit value is the maximum column length in bytes (documentation). With 4 byte length, the largest signed