activerecord

Rails: Model fails to save cached column .counts

淺唱寂寞╮ 提交于 2019-12-12 03:35:53
问题 Ok, this has me wildly stumped. I have a model PhoneNumbers which has an integer column sent_messages_count . I use background tasks to use this column as a counter cache, however, this column's inaccuracies appears to be more fundamental. Here's what I'd like to do: phone_number.sent_messages_count = phone_number.sent_messages.count Note that I'm setting the table column to the count of an association. Underscore vs. period. See my console session... (I've done some clean-up to make it more

Rails adding error to :base not working as expected

房东的猫 提交于 2019-12-12 03:35:44
问题 I have a double nested form: <%= simple_form_for @item, html: { class: "create-item-form" } do |item_builder| %> <div class="well"> <%= item_builder.input :name, required: false, error: false, label: "item name" %> <%= item_builder.input :description, as: :text, required: false, error: false, label: "How do users earn this item?" %> <%= item_builder.input :tag_list, required: false, label: "Tags (these will help users find your item)" %> <%= item_builder.simple_fields_for :user_items do |user

Model Attribute Won't Update, Validation Error Called On Separate Attribute Not Being Updated?

为君一笑 提交于 2019-12-12 03:31:11
问题 I have the following semi-advanced DB query that is going through hourly prices for the last 10 years and returning daily average prices for the last seven days: averages = Trade.where('date >= ?', 7.days.ago).average(:price, :group => "DATE_TRUNC('day', date - INTERVAL '1 hour')") This returns the date (for that day) and an averageprice like this: "2012-12-29 00:00:00"=>#<BigDecimal:7f97932be328,'0.2513458333 33333333E2',27(27)> I then loop through each response and save them as new records

Querying based on two associated records

无人久伴 提交于 2019-12-12 03:29:11
问题 I have a product that has many variants, those variants have two attributes: Size and Color. I want to query for the Variant based on the two attributes I pass in - I got it to work with following: variants = Spree::Variant.joins(:option_values).where(:spree_option_values => {:id => size.id}, :product_id => prod.id).joins(:option_values) variant = variants.select{|v| v.option_values.include?(size)} From my understanding, the select method more or less iterates through the array, which is

How to exclude parts of json, stored in Postgres, when selecting using ActiveRecord?

末鹿安然 提交于 2019-12-12 03:25:23
问题 This is a follow up question to this one: How to select only part of json, stored in Postgres, with ActiveRecord 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'] } } The difference to the question cited above is that I'd like to know how to exclude part of the json. So here, I want to select everything from

Active record callbacks. How are they able to access variables/scope?

对着背影说爱祢 提交于 2019-12-12 03:19:36
问题 I'm new to Ruby on Rails and I'm hung up on using Active Record callbacks. I know how to use them, but I'm trying to truly understand what is happening, and I'm not getting it. My confusion has to do with variable scopes in Ruby. Here is a simple Active Record class for a user with fields: email, password_hash, password_salt class User < ActiveRecord::Base attr_accessor :password before_save :encrypt_password #validation validates :password, :confirmation => true validates :email, :password,

Rails problems with update parameters

瘦欲@ 提交于 2019-12-12 03:15:29
问题 I have a problem, that I can't understand... In my project I has several models. I have channel that has many restreams and each restream has one provider (it's a polymorphic assosiation). So in code I replace one restream.provider with another, and in the end restream.provider has a value of nil . I tried to put a lot of testing output, so now I have such a result: in one of the controllers I have a code: puts channel.restreams.last.provider.to_json byebug Delayed::Job.enqueue

Rails 4: can a child model belong_to two different parent models

落花浮王杯 提交于 2019-12-12 03:13:23
问题 In my initial Rails 4 app, I had the following models: User has_many :administrations has_many :calendars, through: :administrations has_many :comments Calendar has_many :administrations has_many :users, through: :administrations has_many :posts has_many :comments, through: :posts Administration belongs_to :user belongs_to :calendar Post belongs_to :calendar has_many :comments Comment belongs_to :post belongs_to :user I just added a new Ad model to the app: Ad belongs_to :calendar And now I

ActiveRecord::StatementInvalid: ArgumentError: negative string size (or size too big): SELECT * FROM [shop]

独自空忆成欢 提交于 2019-12-12 03:13:10
问题 I'm getting this error when trying to instantiate too much objects in memory. This a Rails 2.3.x app, with SQL Server as the database. >> Shop.count => 14111 >> Shop.all ActiveRecord::StatementInvalid: ArgumentError: negative string size (or size too big): SELECT * FROM [shop] from /Users/lunks/.rvm/gems/ree-1.8.7-2011.03@ums/gems/activerecord-2.3.14/ lib/active_record/connection_adapters/abstract_adapter.rb:227:in `log' from /Users/lunks/.rvm/ gems/ree-1.8.7-2011.03@ums/gems/activerecord

has_many/belongs_to build association - why is insert statement for parameter blank?

左心房为你撑大大i 提交于 2019-12-12 03:09:12
问题 I have the following models role.rb class Role < ActiveRecord::Base attr_accessible :description, :user_id, :role_ids, :permission_ids, :permissions has_many :assignments has_many :users, :through => :assignments has_many :permissions validates :description, presence: true, uniqueness: true end permission.rb class Permission < ActiveRecord::Base attr_accessible :action, :role_id, :subject_class, :subject_id, :role belongs_to :role, :polymorphic => true end My roles_controller.rb is as follows