activerecord

NoMethodError - new since upgrading to Rails 4

会有一股神秘感。 提交于 2019-12-11 17:23:37
问题 I'm at my wit's end. I upgraded to Rails 4.2.10, and everything is terrible. Here is the relevant part of /models/product.rb class Product < ActiveRecord::Base delegate_attributes :price, :is_master, :to => :master And here is /models/variant.rb: class Variant < ActiveRecord::Base belongs_to :product The variants table has fields for "price" and "is_master". Products table does not. It used to be the case that one could access Product.price and it would get/set the price for the master

How to write a query to compare current date to created_at timestamps in database?

一笑奈何 提交于 2019-12-11 17:00:52
问题 I would like to write a query to compare created_at timestamp with my current date in Ruby on Rails using ActiveRecord and I don't quite know how do it yet Date is stored like this -> created_at: 2019-06-03 03:30:40 回答1: Try this: <% if @user.created_at > 5.days.ago.beginning_of_day %> <span>New User!</span> <% end%> Or you can use: <%= time_ago_in_words(@user.created_at) %> Source: https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words 回答2: It's straightforward in Ruby

Rails::ActiveRecord preserve auto_increment id in deep copy duplicate

大城市里の小女人 提交于 2019-12-11 16:55:49
问题 I need to create a temporary array containing model objects to manipulate. I need the auto_increment primary key but I do not want the associations kept because when I remove the object from the array it removes it from the database as well (thanks rails, pretty inconvenient). The deep copies I create using object.dup have the id's marked as nil. How do I keep them in the copy? 回答1: dup describes this behaviour: Duped objects have no id assigned and are treated as new records. The dup method

Get distinct elements from a Rails ActiveRecord query

女生的网名这么多〃 提交于 2019-12-11 16:49:33
问题 I've got a query in Rails like this: @addresses=People.select("first_name, state").where("first_name = 'Bob'") Is there a way I can iterate over addresses and pull out the distinct states, so that I know in which states someone named bob lives? I'm trying to avoid: @states = [] @address.each.do |add| if !@states.contains?(add.state) @states.push add That just seems like bad form. 回答1: @states = People.where(first_name: 'Bob').uniq.pluck(:state) 来源: https://stackoverflow.com/questions/10130818

switching database php activerecord

北战南征 提交于 2019-12-11 16:46:39
问题 I'm using ActiveRecord and I need switching databases. When I log-in I select a database. The databases are the same schema. I tried: $connections = array( '1' => 'mysql://root:pass@localhost/db1;charset=utf8', '2' => 'mysql://root:pass@localhost/db2;charset=utf8', 'test' => 'mysql://root:password@localhost/database_name' ); $current_db = $_SESSION['db'] ? $_SESSION['db'] : '2'; ActiveRecord\Config::initialize(function($cfg) use ($connections, $current_db) { $cfg->set_model_directory(MODEL

Rails error: object does not support #inspect

心已入冬 提交于 2019-12-11 16:44:56
问题 Scratching my head here... If I try the following with rails: User.limit(25).includes([:addresses]) I get: User Load (109.5ms) EXEC sp_executesql N'SELECT TOP (25) [User].* FROM [User]' Address Load (112.3ms) EXEC sp_executesql N'SELECT [Address].* FROM [Address] WHERE [Address].[UserId] IN (N''1'', N''2'', N''3'', N''6'', N''7'', N''8'', N''9'', N''11'', N''12'', N''16'', N''17'', N''18'', N''19'', N''20'', N''21'', N''22'', N''24'', N''25'', N''26'', N''27'', N''28'', N''29'', N''30'', N'

Codeigniter 2 active record — How do I create temporary table to apply second sort order?

自作多情 提交于 2019-12-11 16:26:18
问题 I am in the middle of updating an old site. One of the things I am doing is rewriting the database queries because I have changed the structure of the database to make it more flexible and configurable from the admin. I previously used the following query (and many others like it), which rely on a temporary table: $query = $this->db->query("SELECT * FROM (SELECT * FROM Links WHERE Cat LIKE ('$c') AND Type LIKE ('%$x%') LIMIT $s, $l) AS T ORDER BY $sort $o"); Because the queries have got more

Ruby - Ensure only one class object

断了今生、忘了曾经 提交于 2019-12-11 16:18:37
问题 I have a Model Bot and I would like to ensure that there is only one Bot object in my database. I also need to make sure it is persisted and not tampered with. My original thought was to do this in a migration, one that would follow the :bots table migration. It would include a line that is something like: Bot.all.size == 0 ? Bot.create! : nil Maybe this would prevent the AR object from being messed with in future migrations? BONUS: Would be awesome to be able to have instant and global

Sidekiq not finding records for Rails Active Job

匆匆过客 提交于 2019-12-11 16:12:11
问题 Jobs are queued on after a user is created like so in the model user.rb after_create_commit :profile_photo_job def profile_photo_job message = "Add a profile photo" ReminderJob.set(wait: 1800).perform_later(self.id.to_s, message) end reminder_job.rb class ReminderJob < ApplicationJob queue_as :default def perform(user_id, message) if user_id user = User.find(user_id) end ##sending message notification here end end However, it often throws the following error inside my sidekiq console

Eager load polymorphic

自闭症网瘾萝莉.ら 提交于 2019-12-11 16:01:24
问题 Using Rails 3.2, what's wrong with this code? @reviews = @user.reviews.includes(:user, :reviewable) .where('reviewable_type = ? AND reviewable.shop_type = ?', 'Shop', 'cafe') It raises this error: Can not eagerly load the polymorphic association :reviewable If I remove the reviewable.shop_type = ? condition, it works. How can I filter based on the reviewable_type and reviewable.shop_type (which is actually shop.shop_type )? 回答1: My guess is that your models look like this: class User <