activerecord

Rails 3.1: can't write to column in same migration that adds it

≯℡__Kan透↙ 提交于 2019-12-20 10:26:43
问题 I had an add_column migration that would run fine. However, after running it and firing up a console, I would find the first_name and last_name columns completely empty. I tried using save! instead and it had the same effect--no errors reported. Here's the original: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name =

Connecting to SQL Server with ActiveRecord

人走茶凉 提交于 2019-12-20 10:21:40
问题 Have you ever had to connect to SQL Server with ActiveRecord? Is this possible? Can anyone provide some starting points? 回答1: This what I used: From here: http://github.com/rails-sqlserver/2000-2005-adapter/tree/master Installation First, you will need Ruby DBI and Ruby ODBC. To my knowledge the ADO DBD for DBI is no longer supported. The installation below is not a comprehensive walk thru on how to get all the required moving parts like FreeTDS installed and/or configured. It will also

How to specify a less than today condition on a date in rails active record

坚强是说给别人听的谎言 提交于 2019-12-20 10:18:08
问题 I'm trying to figure out how to pull all the records in my set where their fields "publish" is true and "expires" is less than today. I have the following but i dont think the less than part is working, can someone please point me on the right track? @announcements = Announcement.where(:publish => true, :expires < Date.today) Thanks in advance for your help 回答1: Try this: @announcements = Announcement.where("publish = ? AND expires < ?", true, Date.today) 回答2: Since this comes up first on

How to mock and stub active record before_create callback with factory_girl

 ̄綄美尐妖づ 提交于 2019-12-20 10:14:38
问题 I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call back uses a 3rd party API to make a remote connection. I am using factory girl and would like to stub out this api so that when new factories are built during testing the remote calls are not made. I am using Rspec for mocks and stubs. The problem i'm having is that the Rspec methods are not available within my factories.rb model: class PricePackage < ActiveRecord::Base has_many :users before_create

Dynamically connecting a model to databases in a running app?

大兔子大兔子 提交于 2019-12-20 10:10:56
问题 I've read many of the existing questions/threads on this subject, but keep in mind that none of them have directly addressed my issue. Also keep in mind that this is NOT a situation for database.yml as I won't know the DB info in advance. That said, I need a solution for DYNAMICALLY connecting to multiple databases in a Rails app. My situation is that I have multiple data logging sites, all with a simple data table (EVENTS, TIMESTAMP, VALUE). These sites need to (and will) remain as they are

Rails - Adding Custom Fields at runtime in ActiveRecord

人走茶凉 提交于 2019-12-20 10:10:43
问题 You know how some bug trackers (and other software) allow you to add custom fields? Typically this is done with a data structure that looks something like this: Items ---------- ID | NAME | ITEM_TYPE_ID FieldDefinitions --------------------------------------- ID | ITEM_TYPE_ID | FIELD_NAME | FIELD_TYPE FieldValues --------------------------------------- ID | FIELD_ID | ITEM_ID | VALUE I'm trying to figure out the best way to approach this design in Rails. There will be many models which I

how to validate associated model id?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 10:00:14
问题 I have a student and a course model. Student belongs to course, and course has many students. class Student < ActiveRecord::Base attr_accessible :course_id, :name, :password, :status, :studentID, :year belongs_to :course validates :name, :password, :status, :studentID, :year, :presence =>true validates_associated :course end class Course < ActiveRecord::Base attr_accessible :courseCode, :courseName, :courseYr validates :courseCode,:courseName,:courseYr, :presence => true validates :courseCode

Destroy/Remove database in Rails

假如想象 提交于 2019-12-20 09:53:35
问题 Is it possible to completely remove the database and all migration records etc from an existing application so I can redesign the database from scratch? 回答1: By issuing rake -T you have the following database tasks: rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) rake db

Tricky active record relationships - polymorphic bi-directional self-referential

我只是一个虾纸丫 提交于 2019-12-20 09:45:30
问题 How would you model the references and citations to publications (articles, books, chapters, etc...)? A publication can be an article, book or a chapter and it has many references to other publications and other publications refer to it (call these citations) I need to be able to list the relationships among the publications: The references in a publication and the citations from other publications to this publication My initial understanding is that this would be a polymorphic relationship

Can Rails' Active Record handle SQL aggregate queries?

女生的网名这么多〃 提交于 2019-12-20 09:44:36
问题 Just started learning active record and am wondering how to best retrieve data from multiple tables where an SQL aggregate query is involved. In the following example (from a medical app) I'm looking for the most recent events of various types for each patient (e.g. last visit, last labtest etc). As you can see from the sql query below I'm looking for the max(date) value from a grouped query. I resorted to find_by_sql to do this - however I'd like to see how to do this without using find_by