activerecord

What is the ActiveModel method attribute “_was” used for?

自闭症网瘾萝莉.ら 提交于 2019-12-18 13:07:56
问题 When using autocomplete in the console, I often see " _was " postpended to my attributes. But I can't find any documentation or best practices for usage. What does it do and how should it be used? Example: user.fname has the method user.fname_was Using source_location, I've tracked it down to: active_model/attribute_methods.rb", line 296 but there isn't anything specific. 回答1: That is a part of ActiveModel::Dirty You can see it here https://github.com/rails/rails/blob

Elegant PostgreSQL Group by for Ruby on Rails / ActiveRecord

╄→尐↘猪︶ㄣ 提交于 2019-12-18 13:07:01
问题 Trying to retrieve an array of ActiveRecord Objects grouped by date with PostgreSQL. More specifically I'm trying to translate the following MySQL query: @posts = Post.all(:group => "date(date)", :conditions => ["location_id = ? and published = ?", @location.id, true], :order => "created_at DESC") I am aware that PostgreSQL interpretation of the SQL standard is stricter than MySQL and that consequently this type of query won't work...and have read a number of posts on StackOverflow and

Codeigniter: Select from multiple tables

自闭症网瘾萝莉.ら 提交于 2019-12-18 12:40:50
问题 How can I select rows from two or more tables? I'm setting default fields for a form, and I need values from two tables... My current code reads: $this->CI->db->select('*'); $this->CI->db->from('user_profiles'); $this->CI->db->where('user_id' , $id); $user = $this->CI->db->get(); $user = $user->row_array(); $this->CI->validation->set_default_value($user); 回答1: The example in the User Guide should explain this: $this->db->select('*'); // <-- There is never any reason to write this line! $this-

Rails: join with multiple conditions

痴心易碎 提交于 2019-12-18 12:29:44
问题 I have a simple model like class Interest < ActiveRecord::Base has_and_belongs_to_many :user_profiles end class UserProfile < ActiveRecord::Base has_and_belongs_to_many :interests end When I want to query all the users with a specific interests, that's fairly simple doing UserProfile.joins(:interests).where('interests.id = ?', an_interest) But how can I look for users who have multiple interests? Of course if I do UserProfile.joins(:interests).where('interests.id = ?', an_interest).where(

No connection pool for ActiveRecord::Base

社会主义新天地 提交于 2019-12-18 12:18:52
问题 I'm trying to use rails 4.2.6 to develop an app. I'm trying to use postgres for database. Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error. What could it be? EDIT The pg gem wasn't working properly. I had to comment it before starting the server and then uncomment it from my GemFile afterwards. I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended). I removed ruby 2.3 and set up everything under ruby 2.0

How to create ActiveRecord tableless Model in Rails 3

北慕城南 提交于 2019-12-18 12:15:22
问题 I am trying to create a Active Record tableless Model. My user.rb looks like this class User < ActiveRecord::Base class_inheritable_accessor :columns def self.columns @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null ) end column :name, :text column :exception, :text serialize :exception end When creating the new object in controller @user = User.new I am

How to get the maximum length configured in an ActiveRecord validation?

被刻印的时光 ゝ 提交于 2019-12-18 12:12:44
问题 Given a model: class Person validates_lenght_of :name, :maximum => 50 end I have some view code that shows a countdown and enforces this maximum. However I hard coded the number 50 into that view code. Is there a way to extract this number from the model? Something like: Person.maximum_length_of_name I tried this: Person.validators_on(:name) => [#<ActiveRecord::Validations::UniquenessValidator:0x000001067a9840 @attributes=[:name], @options={:case_sensitive=>true}, @klass=Trigger(id: integer,

Failing validations in join model when using has_many :through

邮差的信 提交于 2019-12-18 12:03:10
问题 My full code can be seen at https://github.com/andyw8/simpleform_examples I have a join model ProductCategory with the following validations: validates :product, presence: true validates :category, presence: true My Product model has the following associations: has_many :product_categories has_many :categories, through: :product_categories When I try to create a new product with a category, the call to @product.save! in the controller fails with: Validation failed: Product categories is

Rails: switch connection on each request but keep a connection pool

六眼飞鱼酱① 提交于 2019-12-18 11:49:04
问题 In our Rails application we need to use different databases depending on the subdomain of the request (different DB per country). Right now we're doing something similar to what's recommended in this question. That is, calling ActiveRecord::Base.establish_connection on each request. But it seems ActiveRecord::Base.establish_connection drops the current connection pool and establishes a new connection each time it's called. I made this quick benchmark to see if there was any significant

Rails PG::UndefinedTable: ERROR: missing FROM-clause entry for table

坚强是说给别人听的谎言 提交于 2019-12-18 11:45:44
问题 I have models class Offer < ActiveRecord::Base belongs_to :agency end class Agency < ActiveRecord::Base has_many :offers end When I do such request - everything is OK @offers = Offer.with_state(:confirmed). includes(:destination, :cruise_line, :ship). paginate(per_page: 10, page: params[:page]).decorate But I want to select only offers that belong to active agencies (column state from agencies table), so I try to do like this: @offers = Offer.with_state(:confirmed). includes(:destination,