activerecord

Ruby - LoadError enc/trans/single_byte

こ雲淡風輕ζ 提交于 2019-12-19 07:09:17
问题 I encountered a weird problem while using ActiveRecord::Store module in my Ruby on Rails app. As I understand, this module use 'serialize' method under the hood so it just serialize your data to yaml format with ruby built-in psych gem. It works OK most of the time, but sometimes I get 500 error with the following message: LoadError (cannot load such file -- enc/trans/single_byte): ~/.rbenv/versions/1.9.3-p286/lib/ruby/1.9.1/psych/visitors/emitter.rb:27:in `write' ~/.rbenv/versions/1.9.3-p286

When to use association extensions vs named scopes?

耗尽温柔 提交于 2019-12-19 06:47:46
问题 From a cursory glance, they appear to be simply two different approaches to the same set of problems, except that named scopes are chainable, while association extensions are not. Can anyone explain further, or provide an example that would be more appropriate for an association extension than a named scope? 回答1: Association extensions are very useful for creating custom methods for creating, updating, etc (not necessarily finding). Because you have access to the proxy_owner, proxy_reflection

When to use association extensions vs named scopes?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 06:46:59
问题 From a cursory glance, they appear to be simply two different approaches to the same set of problems, except that named scopes are chainable, while association extensions are not. Can anyone explain further, or provide an example that would be more appropriate for an association extension than a named scope? 回答1: Association extensions are very useful for creating custom methods for creating, updating, etc (not necessarily finding). Because you have access to the proxy_owner, proxy_reflection

Activeadmin: how to filter for strings that match two or more search terms

你离开我真会死。 提交于 2019-12-19 06:04:48
问题 Let's say I've got User class with an :email field. And let's say I'm using activeadmin to manage Users. Making a filter that returns emails that match one string, e.g. "smith", is very simple. In admin/user.rb , I just include the line filter :email This gives me a filter widget that does the job. However, this filter doesn't let me search for the intersection of multiple terms. I can search for emails containing "smith", but not for emails containing both "smith" AND ".edu". Google tells me

Does ActiveRecord save a belongs_to association when saving main object?

血红的双手。 提交于 2019-12-19 05:44:59
问题 If I have two models: class Post < ActiveRecord::Base belongs_to :user end and class User < ActiveRecord::Base has_many :posts end If I do: post = Post.new user = User.new post.user = user post.save Does the user get saved as well and the primary key properly assigned in post 's user_id field? 回答1: ActiveRecord belongs_to associations have the ability to be autosaved along with the parent model, but the functionality is off by default. To enable it: class Post < ActiveRecord::Base belongs_to

Is there a clean API for resetting instance variables on 'reload' in ActiveRecord?

泪湿孤枕 提交于 2019-12-19 05:10:26
问题 In an ActiveRecord::Base model, I can reset the state of the model to what it was when I got it from the database with reload , as long as the attribute I'm setting maps to a table column: user = User.first user.email #=> "email@domain.com" user.email = "example@site.com" user.email #=> "example@site.com" user.reload user.email #=> "email@domain.com" But if I add a custom attribute, the only way I've found to have it act the same is like this: class User < ActiveRecord::Base attr_accessor

Rails: Calling one Model from another Model. Why is this not possible?

点点圈 提交于 2019-12-19 04:15:11
问题 I have the following Model... class Room < ActiveRecord::Base belongs_to :hotel belongs_to :layout has_many :visits validates :number, presence: true validates :rate, presence: true validates :smoking, presence: true def self.rooms_with_smoking(smoking) self.where('smoking = ?', smoking) end def self.occupied_rooms(from_date, to_date) #24-26 self.joins(:visits).where('date >= ? and date <= ?', from_date, to_date).uniq end def self.vacant_rooms(from_date, to_date) self.where.not(id: Room

Rails: Calling one Model from another Model. Why is this not possible?

亡梦爱人 提交于 2019-12-19 04:15:11
问题 I have the following Model... class Room < ActiveRecord::Base belongs_to :hotel belongs_to :layout has_many :visits validates :number, presence: true validates :rate, presence: true validates :smoking, presence: true def self.rooms_with_smoking(smoking) self.where('smoking = ?', smoking) end def self.occupied_rooms(from_date, to_date) #24-26 self.joins(:visits).where('date >= ? and date <= ?', from_date, to_date).uniq end def self.vacant_rooms(from_date, to_date) self.where.not(id: Room

Polymorphic associations in LINQ to SQL

不羁岁月 提交于 2019-12-19 04:12:33
问题 Does LINQ to SQL provide out-of-the-box polymorphic associations as ruby on rails active record does? If not, is there any workaround to manually map those associations? 回答1: Agreed. I found no possible way of doing this nor using the designer nor by hand appending class/method attributes. Moreover is not possible to have foreign key constraints for polymorphic associations. I discarded this option, thanks. 回答2: EDITTED SQL Server won't allow you to have a foreign key relationship on a column

Custom db entry for 3 way habtm in ROR

孤人 提交于 2019-12-19 03:59:29
问题 I am converting an existing perl gtk app into a ROR app I have a 3 way habtm association model. class CustomerMaster < ActiveRecord::Base has_and_belongs_to_many :address_master, :join_table => "customer_phone_addres" has_and_belongs_to_many :phone_master, :join_table => "customer_phone_address" class AddressMaster < ActiveRecord::Base has_and_belongs_to_many :customer_master, :join_table => "customer_phone_addres" has_and_belongs_to_many :phone_master, :join_table => "customer_phone_addres"