rails-activerecord

Circular dependency detected while autoloading a constant

五迷三道 提交于 2019-12-06 02:57:49
I upgraded Rails from version 3.1.2 (which worked fine) to 4.0, and got stuck with the following error: circular dependency detected while autoloading constant Foo I created a class ProductFactory , where I instantiate different models. For example: p = Foo.new(params) The model "Foo" is not always an ActiveRecord. Could anyone help me with this issue? This kind of issues often happen when you change the version of Rails. You maybe didnt update the gems on the right order. Best I'm aware, circular dependencies error messages usually occur when cascading includes go wrong by recursively

Query on a time range ignoring the date of timestamps

浪尽此生 提交于 2019-12-06 02:29:42
问题 I'm trying to query a purchases table in my rails database (Postgres) and I want to query on time ranges. For example, I'd like to know how many purchases were made between 2 PM and 3 PM across all dates. There is a created_at column in this table but I don't see how to accomplish this without searching for a specific date as well. I've tried: Purchases.where("created_at BETWEEN ? and ?", Time.now - 1.hour, Time.now) But this ultimately will just search for today's date with those times. 回答1:

rails migration: postgresql for md5 of random string as default

。_饼干妹妹 提交于 2019-12-06 02:08:50
Rails 3 + postgresql I want to have a sha of a random string for a default value of a column. So, in my migration I have: t.string :uniqueid, default: md5(random()::text) However i can not get this to actually produce anything, I've used backticks, quotes,etc. From examples that I've seen it seems like that pg function only works in a SELECT statement. Is that accurate? Any ideas on how I could achieve this? Thanks mu is too short Rails will try to interpret this: t.string :uniqueid, default: md5(random()::text) as Ruby code and :default => md5(...) doesn't mean anything in Ruby. If you quote

string vs text using Rails 3.2.* and Postgres - should I always just use text

独自空忆成欢 提交于 2019-12-06 01:21:06
I adopted a Rails app (Rails 3.2 and Postgres 9.4) that has a few Rails strings and we have gone past the 255 limit. This app had previously used MySQL rather than Postgres as backing store. My understanding is that postgres handles strings and text the same. Is this correct? Are there any limitations that we should be aware of before migrating all our Rails strings to texts? Issues of performance are a bit of a concern but not the dominant concern. From the fine manual : Tip : There is no performance difference among these three types, apart from increased storage space when using the blank

What is the purpose of ActiveRecord::Relation#bind?

£可爱£侵袭症+ 提交于 2019-12-06 00:03:45
问题 Just out of curiosity - I was reading the docs of the Relation::QueryMethods module and found that method: def bind(value) relation = clone relation.bind_values += [value] relation end Does anyone know what is this? I tried to find by myself, but failed. UPDATE I tracked down usage of @bind_values to the bottomless depth of ActiveRecord::ConnectionAdapters - the values get passed on and on till low-level SQL statement executions. Seems that the individual adapters may use these. My guess is

Applying aggregate function SUM together with GROUP BY

懵懂的女人 提交于 2019-12-05 16:51:12
What is the best way to execute this query in Active Record / Ruby on Rails SELECT sum(amount) as total_amount FROM payments GROUP BY person_id, product_id I can't seem to chain sum with group method. Do I have to resort to find_by_sql here? mu is too short Something like: Payment.group(:person_id).group(:product_id).sum(:amount) should give you a hash whose keys are [person_id, product_id] arrays and whose values are the sums. You probably don't want to: Payment.group('person_id, product_id').sum(:amount) though, that might give you the right sums but the Hash's keys won't make much sense.

Rails HABTM after_add callback fires before saving primary object

廉价感情. 提交于 2019-12-05 15:24:50
I have two ActiveRecord models having a HABTM relationship with eachother. When I add an AccessUnit through a form that allows zones to be added by checking checkboxes I get an exception that the AccessUnitUpdaterJob can't be enqueued because the access unit passed can't be serialized (due to the fact that the identifier is missing). When manually calling save on the primary object, the issue is resolved but of course this is a workaround and not a proper fix. TLDR; it seems the after_add callback is triggered before the main object is saved. I'm actually unsure if this is a bug in Rails or

next available record id

余生颓废 提交于 2019-12-05 13:41:39
@user = User.new @user.id returns nil but i need to know it before i save. Is it possible ? No, you can't get the ID before saving. The ID number comes from the database but the database won't assign the ID until you call save . All this is assuming that you're using ActiveRecord of course. Douglas YES you can! I had the same question and investigated the docs. The ability to solve this question is very related to your database type in fact. Oracle and Postgresql do have useful functions to easily solve this. For MySQL(oracle) or SkySQL (open-source) it seems more complicated (but still

How to group/select JSON type column (PG::UndefinedFunction: ERROR: could not identify an equality operator for type json)

主宰稳场 提交于 2019-12-05 12:14:02
I wanna do <MODEL>.select("brief_content").group("brief_content") Here's the table scheme , :id => :integer, :content => :json, :brief_content => :json But I got the errors, How can I do, Thanks companyAlarmLog Load (0.9ms) SELECT company_alarm_logs.id, company_alarm_logs.name, company_alarm_logs.utc_time, company_alarm_logs.company_alarm_test_id, company_alarm_logs.brief_content, brief_content FROM "company_alarm_logs" GROUP BY brief_content ORDER BY utc_time E, [2014-06-24T09:40:39.069988 #954] ERROR -- : PG::UndefinedFunction: ERROR: could not identify an equality operator for type json

rails validation of nested attributes for uniqueness when some may be marked for destroy

二次信任 提交于 2019-12-05 10:31:34
I've got the following (sanitized) models: class Person < ActiveRecord::Base attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes has_many :addresses, inverse_of: :person accepts_nested_attributes_for :addresses, allow_destroy: true end class Address < ActiveRecord::Base attr_accessible :zip_code, :street,:house_number, :unique_per_person_government_id belongs_to :person, inverse_of: :addresses validates_uniqueness_of :unique_per_person_government_id, scope: :person_id end The problem is as follows, Lets say Person Joe