问题
I have an absense.html.erb
which works fine in development and production. However when I try to access the page I get the following error:
2013-03-25T18:43:43+00:00 app[web.1]: Started GET "/absence" for 77.100.90.77 at 2013-03-25 18:43:43 +0000 2013-03-25T18:43:43+00:00 app[web.1]: 2013-03-25T18:43:43+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error: ERROR: column "requested" does not exist 2013-03-25T18:43:43+00:00 app[web.1]: LINE 1: ...LECT "holidays".* FROM "holidays" WHERE (state = "requested... 2013-03-25T18:43:43+00:00 app[web.1]:
^ 2013-03-25T18:43:43+00:00 app[web.1]: : SELECT "holidays".* FROM "holidays" WHERE (state = "requested")): 2013-03-25T18:43:43+00:00 app[web.1]: 2013-03-25T18:43:43+00:00 app[web.1]: 2013-03-25T18:43:43+00:00 app[web.1]:
app/controllers/holidays_controller.rb:52:in `absence' 2013-03-25T18:43:43+00:00 heroku[router]: at=info method=GET path=/rota_days host=miuk-portal.herokuapp.com fwd="77.100.90.77" dyno=web.1 queue=0 wait=0ms connect=1ms service=5773ms status=200 bytes=897173
The error points to the following line of my absense method in my controller
def absence
#show the holidays where the approver id matches the current user id
#and state = "requested"'
@user = current_user
if current_user.role? :administrator
# a superadmin can view all current holiday requests
@holidays = Holiday.find(:all, :conditions => 'state = "requested"')
else
#otherwise an admin sees the holiday requests that they are approvers for
@holidays = Holiday.find(:all, :conditions => ["approver_id = #{current_user.id}", "state = requested"])
end
end
I have declared this in my holiday model as shown below:
Holiday.rb
class Holiday < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
belongs_to :type, :class_name => "Type"
belongs_to :approver, :class_name => "User"
before_create :default_values #Before creating holiday set default_values
#before_create :overflow
validates :start_at, :presence => { :message => "must be a valid date/time" }
validates :end_at, :presence => {:message => "must be a valid date/time"}
validate :start_must_be_before_end_date
validate :overflow #Hook method to determine if user holidays are over the set amount
validates_presence_of :end_at, :start_at
attr_accessible :description, :end_at, :start_at, :state, :type_id, :user_id, :color
def length
(self.end_at.to_i - self.start_at.to_i)/(60*60*24)
end
def days_used
( (start_at.to_date)..(end_at.to_date) ).select {|d| (1..5).include?(d.wday) }.size
end
#Validates_presence_of is called to ensure that the start date exisit.
#Start date must be lt or = to end date otherwise throw error.
def start_must_be_before_end_date
errors.add(:start_at, "must be before end date") unless
self.start_at <= self.end_at
end
#Overflow of holidays is validated by calling "validates" L31. Then checks if absent days
#Is gt or = to length
def overflow
errors.add(:overflow, "- You only have #{user.absentdays} days holiday remaining; this absence is #{length} days.") unless
user.absentdays >= length
end
def name
return self.user.name
end
def colors
if state ||= "denied"
return self.color ||= "#C00000"
end
end
private
#Setting default values - before_create is called and sets the following
def default_values
self.state ||= "requested"
self.color ||= "#000000"
self.type_id||= 1
end
end
Don't seem to understand why this works in both development and production and not in heroku. Any ideas?
回答1:
Standard SQL strings use single quotes, double quotes are for identifiers (such as table and column names); PostgreSQL follows the standard here, MySQL and SQLite are less strict, other databases do other things with varying amounts of strictness. In any case, single quotes for SQL string literals should work the same everywhere.
You're using double quotes on an SQL string:
@holidays = Holiday.find(:all, :conditions => 'state = "requested"')
#------------------------------------------------------^---------^
You must single quotes:
@holidays = Holiday.find(:all, :conditions => %q{state = 'requested'})
or modernize it and let ActiveRecord deal with the quoting:
@holidays = Holiday.where(:state => 'requested')
You'll probably need to fix the quoting for this one too:
@holidays = Holiday.find(:all, :conditions => ["approver_id = #{current_user.id}", "state = requested"])
Again, modernizing it is the easiest way:
@holidays = Holiday.where(:approver_id => current_user.id, :state => 'requested')
I'm guessing that you're developing on SQLite but deploying on PostgreSQL. That's a bad idea, always develop and deploy on the same stack.
来源:https://stackoverflow.com/questions/15622552/heroku-activerecordstatementinvalid-pgerror-error-column-requested-do