activerecord

Rails conditional validation in model

时间秒杀一切 提交于 2019-12-30 08:14:16
问题 I have a Rails 3.2.18 app where I'm trying to do some conditional validation on a model. In the call model there are two fields :location_id (which is an association to a list of pre-defined locations) and :location_other (which is a text field where someone could type in a string or in this case an address). What I want to be able to do is use validations when creating a call to where either the :location_id or :location_other is validated to be present. I've read through the Rails

Where do the created_at and updated_at columns come from?

混江龙づ霸主 提交于 2019-12-30 07:58:10
问题 All the tables in the database created by a rails application seem to have created_at and updated_at columns. What creates these? Are they optional, or does something internal rely on them? 回答1: They are created by default when you run the ActiveRecord migration for a model. ActiveRecord automatically populates/updates them when you create or update a model instance (and thus the underlying database table row) respectively. You can remove the columns by removing the t.timestamps line from

What’s does the [5.0] in Rails 5’s ActiveRecord::Migration mean?

与世无争的帅哥 提交于 2019-12-30 07:53:13
问题 A migration I created in a Rails 5 application had 5.0 passed into a method: class CreateVariableKeys < ActiveRecord::Migration[5.0] ... end I would like to know what the [5.0] means. 回答1: It is a class method of ActiveRecord::Migration and is defined here. It allows us to select the version of migrations we wish to use between 4.2 and 5.0 . The method throws a: "Unknown migration version ... " error if an incompatible version is passed as an argument. Production ready versions of

Ruby / Rails array of strings to PostgreSQL insert

纵然是瞬间 提交于 2019-12-30 07:26:07
问题 Currently, I have a rails project where I am trying to go around active record to straight postgres in order to do a large batch create using an array of strings, where the values to not already exist (to avoid duplication). The problem I am facing is trying to escape the string characters that contain ' or (, ect.. in said ruby array to be acceptable to postgres. Example as follows (obviously its not working): Ruby array: array_of_strings = ["example one's value", "example (2) value"] sql =

Porting complicated has_many relationships to Rails >4.1 (without finder_sql)

纵然是瞬间 提交于 2019-12-30 06:53:51
问题 I am porting a Rails app to Rails 4.2. This Rails app contains some rather complex manual SQL code in associations - partly due to DB optimizations (e.g. subselects instead of JOINs), partly due to no feasible alternative at the time of writing (Rails 3.0), partly surely due to lack of knowledge (I hope, at least - that would be easy to solve). Example: An InternalMessage class. Messages can be sent between users (Recipients of an InternalMessage, and 'deletions' of messages, are stored in

ActiveRecord loads binary field incorrectly on Heroku, fine on OSX

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:20:09
问题 I have a rails 3.1 app that stores images in a binary field in a postgresql database (I am aware of potential issues with storing images in a database, but have to do so for now). Everything works fine locally in development mode and specs on OSX, but all images are broken in the app deployed to Heroku. I've verified that the data in the database is correct by pointing my local machine at the same database that the heroku instance uses, and all images displayed correctly. So, the problem

ActiveRecord loads binary field incorrectly on Heroku, fine on OSX

六月ゝ 毕业季﹏ 提交于 2019-12-30 06:20:07
问题 I have a rails 3.1 app that stores images in a binary field in a postgresql database (I am aware of potential issues with storing images in a database, but have to do so for now). Everything works fine locally in development mode and specs on OSX, but all images are broken in the app deployed to Heroku. I've verified that the data in the database is correct by pointing my local machine at the same database that the heroku instance uses, and all images displayed correctly. So, the problem

Rails how to set a temporary variable that's not a database field

扶醉桌前 提交于 2019-12-30 05:48:09
问题 For my app, I have different signup entry points that validate things differently. So in the main signup, nothing is required except for the email and password field. In an alternative signup field, many more are required. So in the user model I have validate_presence_of :blah, :lah, :foo, :bah, :if => :flag_detected def flag_detected !self.flag.nil? end I want to set that flag through the controller. However that flag isn't a database field. I'm just wondering if this is achievable in Rails

Rails ActiveRecord Find / Search by Date

社会主义新天地 提交于 2019-12-30 03:24:47
问题 I am trying to find records by 'created_at' date - the database column type is 'datetime' and I am using the UI DatePicker from jQuery my url look like this: "localhost:3000/users_supported?selected_date=2012-10-31" So far i am doing good :) and my query in controller looks like this: @support_histories = current_agent.support_histories.where(:created_at => Date.parse(params[:selected_date])) How to properly extract records by 'date' only from the 'datetime' db column I tried doing this in

Testing a gem that uses ActiveRecord models

雨燕双飞 提交于 2019-12-30 03:09:12
问题 I wrote a gem that imports data into your database if you pass in an ActiveRecord model. For example: importer = Importer.new(Widget) importer.import(data_source) Is there a good way to test this gem? I would somehow need to connect to a test database and create a test model. Thanks! 回答1: Mostly inspired from this post: http://blog.markstarkman.com/blog/2013/01/23/using-sqlite-to-test-active-record-models/ First, in your gemspec, you can add ActiveRecord and sqlite3 as dependencies like so: