activerecord

HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered

不想你离开。 提交于 2019-12-20 14:23:58
问题 Just ran into an issue with a has_many :through association and after/before-destroy callbacks not being triggered. Say, I have users, groups, and an intermediate relation called membership. I have a form that allows users to be enrolled into groups by creating a new membership record when they check off associated checkboxes. Basically an array of group_ids. Looks something like this: Which group would you like to join? (check all that apply) [] Group A [] Group B [] Group C And I wish to

Rails - Joining multiple tables

落花浮王杯 提交于 2019-12-20 11:50:55
问题 I have the following models: class Company < ActiveRecord::Base has_many :price_movements has_many :goods_movements end class PriceMovement < ActiveRecord::Base belongs_to :company end class GoodsMovement < ActiveRecord::Base belongs_to :company end I am trying to join everything together into an sql in the form of activerecord, but I'm not sure how to go about doing it because I'm relatively new to ROR. select * from companies c inner join price_movements p on c.id = p.company_id inner join

How to setup a one to many relationship?

巧了我就是萌 提交于 2019-12-20 11:19:19
问题 I have the following models: User (id, name, network_id) Network(id, title) What kind of Rails model assoc do I need to add so that I can do: @user.network.title @network.users Thanks 回答1: so network has_many users and a user belongs_to network. Just add a network_id to users table if you still haven't and also since it's a foreign_key is worth indexing it. rails generate migration AddNetworkIdToUsers class AddNetworkIdToUsers < ActiveRecord::Migration def change add_column :users, :network

Rails ActiveRecord Query Date Range

天涯浪子 提交于 2019-12-20 11:04:26
问题 I'm trying to use the following line in my controller to capture all tasks due less than a week from the current date: @due_this_week = current_user.tasks.where(due_date: Date.today..1.week.from_now) For some reason it's not finding any results even I know I have tasks due within four and six days. This is the only instance variable using a range query. I have another one that works fine to find overdue tasks: @overdue = current_user.tasks.where("due_date <= ?", Date.today) What am I missing?

ActiveRecord - replace model validation error with warning

流过昼夜 提交于 2019-12-20 10:47:09
问题 I want to be able to replace a field error with a warning when saving/updating a model in rails. Basically I want to just write a wrapper around the validation methods that'll generate the error, save the model and perhaps be available in a warnings hash (which works just like the errors hash): class Person < ActiveRecord::Base # normal validation validates_presence_of :name # validation with warning validates_numericality_of :age, :only_integer => true, :warning => true # <-- only warn end >

Update db/migrate after manually updating models?

一笑奈何 提交于 2019-12-20 10:47:09
问题 For example i have this model: class Product < ActiveRecord::Base attr_accessible :name, :order end Then when i did rake db:migrate it created this db/migrate/20120825132038_create_products.rb : class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.integer :order t.string :name t.timestamps end end end But it all happend cuz i used rails generate Product order:integer name:string Now after i go to Product model and changes it manually to: class Product <

Rails 4: Export database to YAML

别来无恙 提交于 2019-12-20 10:40:04
问题 With Rails 2/3 it was possible and easy to export (dump) a database to YAML using one of a few plugins (see Best way to export a database table to a YAML file?). However, none of these plugins seem to be compatible with Rails 4. Is there still an easy way to do this? 回答1: The method indicated in the post linked in the question is still valid, but the code is not anymore a plugin: it's the yaml_db gem. The repository is at https://github.com/yamldb/yaml_db To dump the db, add the gem to the

Refactoring ActiveRecord models with a base class versus a base module

南笙酒味 提交于 2019-12-20 10:36:32
问题 Class A and B are identical: class A < ActiveRecord::Base def foo puts "foo" end end class B < ActiveRecord::Base def foo puts "foo" end end What's the difference between refactoring like this with a base class : class Base < ActiveRecord::Base def foo puts "foo" end end class A < Base end class B < Base end versus like this using a base module : module Base def foo puts "foo" end end class A < ActiveRecord::Base include Base end class B < ActiveRecord::Base include Base end Is one way

disable explain in Rails 3.2

南笙酒味 提交于 2019-12-20 10:33:26
问题 Is it possible to disable the new the explain functionality in Rails 3.2 globally via configuration? I'm using activerecord-sqlserver-adapter 3.2.1 and there appear to be some bugs with the explain (show plan) portion of the gem. 回答1: To cite from http://weblog.rubyonrails.org/2011/12/6/what-s-new-in-edge-rails-explain/ New applications get config.active_record.auto_explain_threshold_in_seconds = 0.5 in config/environments/development.rb . Active Record monitors queries and if they take more

Dynamically defined setter methods using define_method?

被刻印的时光 ゝ 提交于 2019-12-20 10:30:56
问题 I use a lot of iterations to define convenience methods in my models, stuff like: PET_NAMES.each do |pn| define_method(pn) do ... ... end but I've never been able to dynamically define setter methods, ie: def pet_name=(name) ... end using define_method like so: define_method("pet_name=(name)") do ... end Any ideas? Thanks in advance. 回答1: Here's a fairly full example of using define_method in a module that you use to extend your class: module VerboseSetter def make_verbose_setter(*names)