activerecord

How to select only part of json, stored in Postgres, with ActiveRecord

邮差的信 提交于 2020-01-01 10:08:33
问题 Say I have a model User , which has a field of type json called settings . Let's assume that this field looks roughly like this: { color: 'red', language: 'English', subitems: { item1: true, item2: 43, item3: ['foo', 'bar', 'baz'] } } If I do User.select(:settings) I will get all the settings for each user. But I want to get only the languages for a user. I tried both: User.select("settings -> 'language'") and User.select("settings ->> 'language'") but this just returns empty objects: [#<User

add a database column with Rails migration and populate it based on another column

半世苍凉 提交于 2020-01-01 09:22:11
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(

add a database column with Rails migration and populate it based on another column

浪子不回头ぞ 提交于 2020-01-01 09:22:09
问题 I'm writing a migration to add a column to a table. The value of the column is dependent on the value of two more existing columns. What is the best/fastest way to do this? Currently I have this but not sure if it's the best way since the groups table is can be very large. class AddColorToGroup < ActiveRecord::Migration def self.up add_column :groups, :color, :string Groups = Group.all.each do |g| c = "red" if g.is_active && is_live c = "green" if g.is_active c = "orange" g.update_attribute(

ActiveRecord: Do I need both belongs_to and has_one

你。 提交于 2020-01-01 08:36:31
问题 I have 2 models, namely user and userprofile . There is a one-to-one relationship between user and userprofile. class Userprofile < ActiveRecord::Base attr_accessible :fname, :lname, :iswoman, :age, :urlphoto, :user_id belongs_to: user end class User < ActiveRecord::Base attr_accessible :name, :provider, :uid has_one: userprofile end I'd like to know whether I need both class to set the connection or having just either belongs_to or has_one is enough? The same is true for the other methods,

Rails destroy all but newest n records

瘦欲@ 提交于 2020-01-01 07:35:10
问题 How do I destroy all but the newest n records using Rails' ActiveRecord? I can get the newest n records using order and limit but how do I destroy the inverse? 回答1: Either of these methods would do it: # Fetch your latest N records newest_n_records = Foo.find(:all, :order => 'created_at DESC', :limit => n) # Then do: Foo.destroy_all(['id NOT IN (?)', newest_n_records.collect(&:id)]) # Or: Foo.destroy_all('created_at < ?', newest_n_records.last.created_at) 回答2: I have two methods of doing this

how to make ActiveRecord bump updated_at on new has_many association

久未见 提交于 2020-01-01 07:31:08
问题 When adding records to a has_many association and saving the parent, that parent's updated_at is not bumped: order = Order.new order.save pp order => #<Order id: 3, created_at: "2013-04-18 15:25:09", updated_at: "2013-04-18 15:25:09"> order.line_items << LineItem.new() pp order.line_items => [#<LineItem id: 4, order_id: 3, created_at: "2013-04-18 15:26:16", updated_at: "2013-04-18 15:26:29", product_id: 123>] order.save pp order.reload => #<Order id: 3, created_at: "2013-04-18 15:25:09",

Code Igniter - remove single quotes from where_in

有些话、适合烂在心里 提交于 2020-01-01 05:54:10
问题 I have 2 queries: $genres = $this->db->select('Group_Concat(intGenreId) strDJGenres') ->from('tblDJGenres') ->where('intDJId', $this->session->userdata('non_admin_userid')) ->get() ->row(); $results = $this->db->select('tblTracks.*, tblGenres.strName as strGenreName') ->from('tblTracks') ->join('tblGenres', 'tblTracks.intGenreId = tblGenres.intGenreId', 'left') ->where_in('tblTracks.intGenreId', $genres->strDJGenres) ->get() ->result(); The first query is returning a string such as '1,2,3,4,8

How to save data with has_many :through

不想你离开。 提交于 2020-01-01 05:44:07
问题 I have many-to-many relationship between Game and Account models like below: class Account < ActiveRecord::Base has_many :account_games, :dependent => :destroy has_many :games, :through => :account_games end class Game < ActiveRecord::Base has_many :account_games, :dependent => :destroy has_many :accounts, :through => :account_games end class AccountGame < ActiveRecord::Base belongs_to :account belongs_to :game end Now I know let's say I want to create a record something like: @account =

ActiveRecord Global Callbacks for all Models

邮差的信 提交于 2020-01-01 05:35:44
问题 I have around 40 models in my RoR application. I want to setup a after_save callback for all models. One way is to add it to all models. Since this callback has the same code to run, is there a way to define it globally once so that it gets invoked for all models. I tried this with no luck: class ActiveRecord::Base after_save :do_something def do_something # .... end end Same code works if I do it in individual models. Thanks, Imran 回答1: You should use observers for this: class AuditObserver

validates_presence_of with belongs_to associations, the right way

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 05:26:10
问题 I'm investigating on how validates_presence_of actually works. Suppose I have two models class Project < ActiveRecord::Base [...] has_many :roles end and class Role < ActiveRecord::Base validates_presence_of :name, :project belongs_to :project end I want it so that role always belongs to an existing project but I just found out from this example that this could lead to invalid (orphaned) roles saved into the db. So the right way to do that is to insert the validates_presence_of :project_id in