activerecord

ActiveRecord nested SELECT — can I do it without manual SQL?

夙愿已清 提交于 2019-12-22 16:08:07
问题 I have a table with (among other things) a name and a rank. I'd like to return the set of all unique names, but for each name returned, I'd like to pick the row with the highest rank. This is simple with two nested SELECT statements: SELECT * FROM (SELECT * FROM foo ORDER BY rank DESC) AS ordered GROUP BY name MySQL takes the first "hit" for each name, which (because of the earlier ORDER BY) will always be the highest-ranking one. Now, if I want to wire into this table with ActiveRecord, I'm

How to set multi select value from array object in yii2 while updating

可紊 提交于 2019-12-22 14:58:27
问题 I have table which have multiple reference to ohter tables like user id name email categories id title user_categories user_id category_id Here a user will have multiple category associated with him/her I am able to save these successfully with new records like following View File: echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHelper::map( StudyMaterialCategories::find()->all(), 'id', 'title'), ['multiple' => true] ); Save New record: $model = new Packages();

How can I use ActiveRecord on a database that has a column named 'attribute'? (DangerousAttributeError)

你。 提交于 2019-12-22 14:02:23
问题 I am accessing a database that I can't change and it has a column named attribute defined. Anytime I try to access an attribute , I get this exception: attribute? is defined by ActiveRecord(ActiveRecord::DangerousAttributeError) my code: class User < ActiveRecord::Base def self.authenticate(username,password) where(:username => username, :value => password).first end end I found a plan on the ruby on rails mailing list for fix the problem but not work for me class << self def instance_method

Automatically update created_by and updated_by value in Ruby on Rails

走远了吗. 提交于 2019-12-22 13:55:16
问题 I'm trying to add the current user_id into a created_by and updated_by field automatically. Can anyone help me? Here is data schema: create_table "businesses", force: :cascade do |t| t.string "business_name" t.string "last_name" t.date "start_date" t.date "end_date" t.integer "created_by" t.integer "modified_by" t.datetime "created_at", null: false t.datetime "updated_at", null: false end 回答1: First of all, current_user is in your controller , there isn't any directed way to get that data

How do I sort a “foreign” column using datatable/will_paginate as in Railscast 340?

时光毁灭记忆、已成空白 提交于 2019-12-22 13:46:08
问题 Environment: Ruby 2.0.0, Rails 4.0.3, Windows 8.1, PostreSQL, Datatable 1.12.2, Will_Paginate 3.0.5 I’ve successfully implemented the Railscast 340 solution, thanks to help provided here. However, my table has columns that are not native to the displayed table. These columns are polymorphic relationships using has_many through. The table being displayed is: class Product < ActiveRecord::Base has_one :location, dependent: :destroy has_one :patron, through: :location, source: :locator, source

How to group and count by day in Rails in Postgres?

元气小坏坏 提交于 2019-12-22 12:47:26
问题 This SO question How do I group by day instead of date? shows how to group in Ruby. This works, but is going to be very slow for a large number of records. How to do it so that the grouping and counting by :created_at by day happens within Postgres? 回答1: If you haven't an order scope defined, you can use date() method from postgres, like this: Post.select("date(created_at) as created_date").group("created_date") And if you do have an order scope: Post.all.except(:order). select("date(created

update_all through an association

£可爱£侵袭症+ 提交于 2019-12-22 12:37:29
问题 I am trying to use update_all through an association, and i am getting mysql errors, anyone know why please? class Basket < ActiveRecord::Base has_many :basket_items has_many :articles, :through => :basket_items def activate_articles articles.update_all :active => true end end class BasketItem < ActiveRecord::Base belongs_to :basket belongs_to :item belongs_to :article end Mysql::Error: Unknown column 'basket_items.basket_id' in 'where clause': UPDATE `articles` SET `active` = 1 WHERE ((

Postgres ORDER BY values in IN list using Rails Active Record

橙三吉。 提交于 2019-12-22 12:19:15
问题 I receive a list of UserIds(about 1000 at a time) sorted by 'Income'. I have User records in "my system's database" but the 'Income' column is not there. I want to retrieve the Users from "my system's database" in the Sorted Order as received in the list. I tried doing the following using Active Record expecting that the records would be retrieved in the same order as in the Sorted List but it does not work. //PSEUDO CODE User.all(:conditions => {:id => [SORTED LIST]}) I found an answer to a

Rails: Serialize value as comma-separated and not as YAML

久未见 提交于 2019-12-22 11:34:06
问题 I'm looking for a way to store a serialized value of eg. IDs in a column. In before claims that this is not an optimal design: the column is used for IDs of associated records, but will only be used when displaying the record - so no queries are made with selection on the column and no joins will be made on this column either. In Rails I can serialize the column by using: class Activity serialize :data end This encodes the column as YAML. For legacy sake and since I'm only storing one

association and migration between users and teams (rails)

只愿长相守 提交于 2019-12-22 11:30:31
问题 I have this User and team model which has the following association: user.rb class User < ActiveRecord::Base belongs_to :team team.rb class Team < ActiveRecord::Base has_many :users has_one :leader, class_name: "User", foreign_key: "leader_id" belongs_to :manager, class_name: "User", foreign_key: "manager_id" but it seems that I can't imagine representing it properly into a migration. At first, this is what I did: class AddTeamIdToUsers < ActiveRecord::Migration def change add_column :users,