activerecord

acts_as_taggable_on: how to optimize the query?

£可爱£侵袭症+ 提交于 2019-12-09 05:37:15
问题 I use acts_as_taggable_on in my current Rails project. On one overview page i am displaying an index of objects with their associated tags. I use the following code: class Project < ActiveRecord::Base acts_as_taggable_on :categories end class ProjectsController < ApplicationController def index @projects = Project.all end end # in the view <% @projects.each do |p| %> <%= p.name %> <% p.category_list.each do |t| %> <%= t %> <% end %> <% end %> This all works as expected. However, if i am

Rails 3 DateTime Comparison w/ Date In An ActiveRecord Query

*爱你&永不变心* 提交于 2019-12-09 05:34:46
问题 I'm trying to search a model for any dates equal to a specific date while omitting the timestamp. In Rails I could simply execute this as DateTime.to_date == somedate , however I don't think it's quite as easy to formulate in SQL where I wouldn't be able to apply the to_date method to an entire column like created_at: Foo.where("created_at == some_day_without_time_stamp").count Initially I thought that because I was using a postgresql database I could simply use psql syntax, but I much much

Why does active record pattern not work with rich domain models?

﹥>﹥吖頭↗ 提交于 2019-12-09 05:29:46
问题 I'm reading the architectural patterns chapter of POEAA, and Fowler says that "As the domain logic gets more complicated and you begin moving toward a rich Domain Model (116), the simple approach of an Active Record (160) starts to break down. The one-to-one match of domain classes to tables starts to fail as you factor domain logic into smaller classes. Relational databases don't handle inheritance, so it becomes difficult to use strategies [Gang of Four] and other neat OO patterns. As the

What are all possible keys for database.yml

我是研究僧i 提交于 2019-12-09 05:22:13
问题 I've just discovered that the reconnect: true configuration option is possible in the database.yml file. What other possible configuration options are there? Is there a complete reference for all options? Known key examples: default: &default adapter: mysql2 encoding: utf8 pool: 5 username: foo password: bar reconnect: true socket: /var/sock/thing.sock development: <<: *default database: app_development 回答1: I don't think there is any place that just lists them but I checked the ActiveRecord

Rails : uninitialized constant error on Active Record destroy

时光总嘲笑我的痴心妄想 提交于 2019-12-09 05:15:25
问题 I am having an issue when trying to destroy an active record instance. It involves the following AR class Client < ActiveRecord::Base has_many :phone_numbers, :dependent => :destroy has_many :email_addresses, :dependent => :destroy has_many :user_clients , :dependent => :destroy has_many :users, :through => :user_clients end class UserClient < ActiveRecord::Base belongs_to :user belongs_to :client , :dependent => :destroy has_many :instructions, :dependent => :destroy end When performing a

Rails 4 Update Nested Attributes

。_饼干妹妹 提交于 2019-12-09 05:06:16
问题 Updating Nested Attributes append instead of updating in has many relationships I am trying to use Rails 4 Update_attributes Class Person <ActiveRecord::Base has_many :pets accepts_nested_attributes_for :pets end Class Pet < ActiveRecord::Base belongs_to :person end In my controller I am recieveing the params as {id: 23, house_no:'22A', pets: [{name:'jeffy', type:'dog'}, {name:'sharky', type:'fish'}]} and my update method is def update @Person = Person.find(params[:id]) if @Person.update

global methods in ruby on rails models

孤街浪徒 提交于 2019-12-09 04:24:59
问题 I have methods in all of my models that look like this: def formatted_start_date start_date ? start_date.to_s(:date) : nil end I would like to have something that automatically writes a method like this for each datetime field in each model, what's the best way to do this? -C 回答1: I just had to answer this, cos it's a fun Ruby excercise. Adding methods to a class can be done many ways, but one of the neatest ways is to use some of the reflection and evaluation features of Ruby. Create this

“merging” multiple models. To create a “recent activity” box

喜你入骨 提交于 2019-12-09 04:08:35
问题 How do you merge models so that I can have the last 10 Posts, Feed Entries and Private Messages displayed in order? Posts are stored in the "Post" model and ordered on "created_at" Feed Entries are stored in "Planet" and ordered on "published_at" Private Messages are stored in "Message" and need to be filtered with: :conditions => "receiver_id = #{current_user.id}" and ordered on "created_at" 回答1: You have to: query elements for each model merge them in a common format sort and limit Here is

MYSQL multiple insert in codeigniter [duplicate]

旧城冷巷雨未停 提交于 2019-12-09 00:19:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: insert multiple rows via a php array into mysql I know about the possibility of making a multiple insert in mySQL by doing something like this: foreach ($array as $manuf) { $sql[] = '("'.mysql_real_escape_string($manuf['name']).'", "'.$manuf['lang'].'", "'.$mId.'")'; } $this->db->query('INSERT INTO manufacturers (name, lang ,mid) VALUES ' . implode(',', $sql) ); I wonder if there's a better way to do this and

Yii - how can I search by a column from foreign/related key on admin page?

元气小坏坏 提交于 2019-12-09 00:18:47
问题 In my database, I have two tables, manufacturer and plastic: CREATE TABLE `manufacturer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 CREATE TABLE `plastic` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL DEFAULT '', `manufacturer_id` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`,`manufacturer_id`), KEY `manufacturer_id` (`manufacturer_id`),