activerecord

superclass mismatch for class User - inheriting from ActiveRecord::Base

∥☆過路亽.° 提交于 2020-01-05 04:29:07
问题 I am trying to figure out my superclass mismatch error. All the posts I've read about this describe the problem as being that User is defined twice as a class in my application. In my case, it isn't defined twice. I have a services folder and within that I have a user folder (for user service classes). In that user folder, I have a file called organisation_mapper_service.rb, with: class User < ActiveRecord::Base class OrganisationMapperService def self.call(user: u) new(user: user).call end

superclass mismatch for class User - inheriting from ActiveRecord::Base

孤者浪人 提交于 2020-01-05 04:29:06
问题 I am trying to figure out my superclass mismatch error. All the posts I've read about this describe the problem as being that User is defined twice as a class in my application. In my case, it isn't defined twice. I have a services folder and within that I have a user folder (for user service classes). In that user folder, I have a file called organisation_mapper_service.rb, with: class User < ActiveRecord::Base class OrganisationMapperService def self.call(user: u) new(user: user).call end

Rails 5 Active Record - is it possible to keep a table in memory?

╄→гoц情女王★ 提交于 2020-01-05 04:18:35
问题 If we have a small table which contains relatively static data, is it possible to have Active Record load this in on startup of the app and never have to hit the database for this data? Note, that ideally I would like this data to be join-able from other Models which have relationships to it. An example might be a list of countries with their telephone number prefix - this list is unlikely to change, and if it did it would be changed by an admin. Other tables might have relationships with

Obfuscating ids in Rails app

岁酱吖の 提交于 2020-01-05 04:14:24
问题 I'm trying to obfuscate all the ids that leave the server, i.e., ids appearing in URLs and in the HTML output. I've written a simple Base62 lib that has the methods encode and decode. Defining—or better—overwriting the id method of an ActiveRecord to return the encoded version of the id and adjusting the controller to load the resource with the decoded params[:id] gives me the desired result. The ids now are base62 encoded in the urls and the response displays the correct resource. Now I

Activerecord callback, previous_changes vs. changes

十年热恋 提交于 2020-01-05 04:06:16
问题 I'm trying to understand what the difference between these two methods is. Here is the documentation for each: https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/previous_changes https://apidock.com/rails/v4.2.7/ActiveModel/Dirty/changes It appears to me after reading this documentation that previous_changes is what was changed after the changes are done, meaning in an after_* filter, while changes is what will be changed, meaning it's useful in a before_* filter. Am I misunderstanding this?

How can I prevent ActiveRecord from writing to the DB?

帅比萌擦擦* 提交于 2020-01-05 03:43:12
问题 I have a somewhat special use case, where I'd like to create a method that accepts a block, such that anything that happens inside that block is not written to the DB. The obvious answer is to use transactions like so: def no_db ActiveRecord::Base.transaction do yield raise ActiveRecord::Rollback end end But the trouble is that if my no_db method is used inside of another transaction block, then I'll ned up in the case of nested transactions. The drawback here is that nested transactions are

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

霸气de小男生 提交于 2020-01-05 03:33:06
问题 I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console , I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attribute's true value is foo.com) However, if I have a form_for block, and do something like form.text_field :url , it will not return the url with http:// prefixed;

Using ActiveRecord and Rails to insert Data into postgresql database get this error: RuntimeError: ERROR C22003 Minteger o

♀尐吖头ヾ 提交于 2020-01-05 02:52:29
问题 I am using Ruby 1.8.6 and rails 2.1.1 (I have tested rails 2.2.2 as well). I have been using Sqlite3 and I recently migrated to Postgresql 8.2.6 for production use. Now this error shows up. The interesting thing is that when I copy the insert statment and use psql to directly insert it into postgres it works fine. I guess the problem is somewhere on the rails level. But, I have no idea what it is. So if anybody knows what the problem is or how to fix it please let me know. RuntimeError: ERROR

Rails 4: how to access an attribute of an ActiveRecord_Relation-Object?

大城市里の小女人 提交于 2020-01-05 02:44:43
问题 i have a country model and a travel note model. A country has many travel notes and a travel note belongs to one country. in Rails console: TravelNote.published.country(248) [#<TravelNote id: 172, country_id: 248, status: 1, advice_against: 0, published_at: "2012-10-04 07:57:00", created_at: "2014-09-23 09:09:20", updated_at: "2014-09-23 09:09:20">] TravelNote.published.country(248).published_at NameError: undefined local variable or method `published_at' for #<Class:0x00000005a84968> How can

PostgreSQL and ActiveRecord subselect for race condition

白昼怎懂夜的黑 提交于 2020-01-05 01:10:34
问题 I'm experiencing a race condition in ActiveRecord with PostgreSQL where I'm reading a value then incrementing it and inserting a new record: num = Foo.where(bar_id: 42).maximum(:number) Foo.create!({ bar_id: 42, number: num + 1 }) At scale, multiple threads will simultaneously read then write the same value of number . Wrapping this in a transaction doesn't fix the race condition because the SELECT doesn't lock the table. I can't use an auto increment, because number is not unique, it's only