activerecord

Getting Table doesn't exist error, but the table does exist (ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

这一生的挚爱 提交于 2019-12-22 03:45:59
问题 I am getting the following error when I try to access one of my pages ActiveRecord::StatementInvalid (Mysql2::Error: Table 'p478r679_partybot.secretsanta' doesn't exist: SHOW FIELDS FROM 'secretsanta'): app/controllers/secretsantas_controller.rb:7:in `index' But this table exists in my DB. Just to give you a background of my problem, I wrote this application few years ago and it was working fine up until recently. When I tried to start the application with thin server, I got the following

Rails: validating a field is present only if another is present

喜夏-厌秋 提交于 2019-12-22 03:23:37
问题 I have a model where there are two fields that can technically be null. The field names are :is_activated and :activated_at . :activated_at is only required if :is_activated is set to true . It does not need to be present if :is_activated is false . What's the appropriate way in Rails to set this validation directly into ActiveRecord? 回答1: You can use a Proc on the :activated_at validator. validates :activated_at, :presence, if: Proc.new { |a| a.is_activated? } Recommended Reading: http:/

RangeError for simple integer assignment in Rails 4.2.0 that should be caught by validation

蹲街弑〆低调 提交于 2019-12-22 03:15:34
问题 * UPDATE: this is now fixed in 4.2.stable and 4.2.1 * in Rails 4.2.0 (and current 4.2.stable), the ensure_in_range method happens before AR validation, yielding a RangeError if I do something as simple as @obj.threshold = 10_000_000_000 on a column with a postgres type integer threshold | integer | it yields RangeError: 10000000000 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 4 from .../2.0.0-p598/lib/ruby/gems/2.0.0/bundler/gems/rails-62e9e61f2d1b

rails 3/postgres - how long is a string if you don't apply :limit in schema

时光怂恿深爱的人放手 提交于 2019-12-22 02:05:40
问题 My googlefu must be weak because I cannot find anything to tell me the default limit of a string column in my Rails app (hosted at Heroku, using PostgreSQL as the database). Any help would be appreciated! 回答1: ActiveRecord uses varchar(255) (or character varying (255) to be pedantic) if you don't specify a specific limit. You can always hop into PostgreSQL with psql and say \d your_table to get the table as PostgreSQL sees it. I don't think the default is specified anywhere but it is right

Difference between Active Record and DAO?

心不动则不痛 提交于 2019-12-22 01:25:30
问题 What's the difference between a Data Access Object and Active Record? They seem to be quite the same, as both built a layer between the application and persistence layer, and abstract away direct database access using SQL queries. 回答1: Data Access Object (DAO) refers to an object in your data layer responsible for persisting a separate entity in your domain. Active Record is a specific method of doing a DAO where the class containing the values of a single row from a table is also responsible

Rails - Find records that are not present another join table

最后都变了- 提交于 2019-12-22 01:08:26
问题 I'm trying to write a ActiveRecord statment where I'm looking for all records where the id isn't present in another table... Whats the syntax? @events = Event.find(:all, :include => :personals, :conditions => ["event.id != ? ", @user.personal.event_id ]) Where personals is a join table that has the user_id and a event_id.... SO i'm essentially trying to find every event record that the user hasn't added to their own set of personal records.... Is there a better way to write this.... not null

ActiveRecord after_initialize with select

被刻印的时光 ゝ 提交于 2019-12-22 00:25:31
问题 With two of my Rails models when I try to call the ActiveRecord method select it just returns the error object doesn't support #inspect These models use the after_initialize callback, so I suspect it has something to do with that. I'm using the after_initialize callback like this: enum state: [ :archived, :active ] after_initialize :state_init after_initialize :date_init def state_init self.state ||= :active end def date_init self.report_date ||= self.event ? self.event.event_date : Date

How to convert this raw query to Active record query interface or arel?

别等时光非礼了梦想. 提交于 2019-12-22 00:13:15
问题 I want to find courses which have at least 1 variant with variant_type = 1 and don't have any variant with variant_type = 2. So my query like: Course.where("id IN ( SELECT course_id FROM course_variants WHERE variant_type = 1 ) AND id NOT IN ( SELECT course_id FROM course_variants WHERE variant_type = 2 )") Additionally, a course has many course_variants . I'm using a raw query in where clause, I want to improve this using Active record interface or Arel, any solution for this? Thank you!

Rails ActiveRecord: Three Table has_many through: associations

旧城冷巷雨未停 提交于 2019-12-21 23:45:59
问题 I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids

find all children of an object tagged with acts_as_taggable_on

為{幸葍}努か 提交于 2019-12-21 22:52:05
问题 I have a rails site with two models: events and dates events has many dates events are tagged using acts_as_taggable_on I'm trying to find all dates (chronological order) for events tagged with a specific tag. I can figure out how to get all the tagged events, but I can't figure out how to get all of the dates. The end result I'm looking for is to create a calendar of similar events where events appear on each of their dates. Hopefully that makes sense, thanks for any and all input! 回答1: As