activerecord

Codeigniter Active Record (and not only that) peculiar problem on some server

拥有回忆 提交于 2019-12-25 15:01:19
问题 I'm experiencing a strange problem while using CodeIgniter's Active Record class ( looks like not only that, check update at bottom of question ), and that happens only on some servers (belonging to the same provider). What happens is that the query is run only "partially", meaning that results are retrieved from mysql database, but any where or join or select clause is ignored. A simple example: $this->db->select('title,intro'); $this->db->from('content'); $this->db->where('published','1');

n:m self-join with ruby on rails active record

被刻印的时光 ゝ 提交于 2019-12-25 12:55:23
问题 I'm trying to accomplish the following with ruby on rails, and I have problems to get the model-configuration right: I would like to model possible connections between airports. I created the following models: > rails generate model airport city name > rails generate model connection airport_id destination_id But most of the n:m association examples deal with associations between two different models on the one hand, or self-referencing models on the other hand, but I want to model a n:m

How to run a callback function on two attributes that depend on each other?

落爺英雄遲暮 提交于 2019-12-25 12:38:47
问题 In my Rails app I have an Invoice model with the attributes date and due_date . For reasons of simplicity I don't want the user to manually enter the due_date but rather simply enter the number of days that should be added to the date . This is why I set up a virtual attribute days_allowed . class Invoice < ActiveRecord::Base belongs_to :user before_save :save_date attr_accessor :days_allowed def days_allowed # attribute reader (I need that too!) (due_date - date).to_i end def save_date self

Check if association exists without incurring a database hit

天涯浪子 提交于 2019-12-25 11:45:29
问题 Is there a way to check if an ActiveRecord's belongs_to association exists without incurring a database query. I'm using example_association.present? to check and it results in the association being loaded if it does. All I want to know is if the association exists. 回答1: You could use reflect_on_all_associations as: Foo.reflect_on_all_associations(:belongs_to).map(&:name).include?(:example_assoc) Where :example_assoc is one of the belongs_to association. Or if you have an instance of model

rake test not copying development postgres db with sequences

风格不统一 提交于 2019-12-25 10:16:11
问题 I am trying to develop a rails application on postgresql using a sequence to increment a field instead of a default ruby approach based on validates_uniqueness_of. This has proved challenging for a number of reasons: 1. This is a migration of an existing table, not a new table or column 2. Using parameter :default => "nextval('seq')" didn't work because it tries to set it in parenthesis 3. Eventually got migration working in 2 steps: change_column :work_commencement_orders, :wco_number_suffix

Rails 4 & ActiveRecord: prevent dependent destroy if owner exists

我是研究僧i 提交于 2019-12-25 09:39:28
问题 Given the following classes: class User < ActiveRecord::Base has_one :profile, dependent: :destroy end class Profile < ActiveRecord::Base belongs_to :user end How do I prevent ActiveRecord from destroying a profile which owner user exists? I mean, it should not be possible to destroy a profile if there's a user who owns it. I did in this way: class User < ActiveRecord::Base has_one :profile after_destroy :destroy_profile private def destroy_profile profile.destroy end end class Profile <

rails - updating active record relation with multiple changed properties

半世苍凉 提交于 2019-12-25 09:38:13
问题 I have an active record relation defined like this: contacts = Contact.where("status = 'waiting'") I then run an .each loop and change properties of contacts depending on some logic. I don't want to do a save! on each individual contact... how can I better save the entire contacts relation after the loop? contacts.each do |contact| //Change properties of contact here end How do I save the newly updates contacts activerecord relation after it contains its new properties? The most efficient way

The case of the disappearing ActiveRecord attribute

邮差的信 提交于 2019-12-25 09:28:16
问题 Following the instructions in https://stackoverflow.com/a/24496452/102675 I wound up with the following: namespace :db do desc 'Drop, create, migrate, seed and populate sample data' task seed_sample_data: [:drop, :create, :migrate, :seed, :populate_sample_data] do puts 'Sample Data Populated. Ready to go!' end desc 'Populate the database with sample data' task populate_sample_data: :environment do puts Inspector.column_names.include?('how_to_fix') # create my sample data end end As you would

ActiveRecord: Handling DB races among workers

…衆ロ難τιáo~ 提交于 2019-12-25 09:26:47
问题 I have a Rails 3 project running on top of PostgreSQL 9.0. Use Case: Users can request to follow Artists by name. To do this, they submit a list of names to a REST resource. If I can't find the Artist by name in the local collection, I consult last.fm for information about them, and cache that information locally. This process can take some time, so it is delegated to a background job called IndexArtistJob . Problem : IndexArtistJob will be run in parallel. Thus, it is possible that two users

How do I destroy a ActiveModel instance with dependant destroy children that have a default ordering?

佐手、 提交于 2019-12-25 09:21:59
问题 In Rails 4.0 I have 2 ActiveRecord classes: class Sequence < ActiveRecord::Base has_many :steps, dependent: :destroy end and class Steps < ActiveRecord::Base belongs_to :sequence default_scope -> { order('order ASC') } end When I call mySequence.destroy I get this error: PG::SyntaxError: ERROR: syntax error at or near "order" LINE 1: ...steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC ^ : SELECT "steps".* FROM "steps" WHERE "steps"."sequence_id" = $1 ORDER BY order ASC When I remove