Topic: deployment errors.

Okay I'm trying to deploy a site and I'm getting an error.

We're sorry, but something went wrong.
We've been notified about this issue and we'll take a look at it shortly.

the production log shows this

ActionView::TemplateError (Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'privileges ON roles.id = privileges.role_id    WHERE ((privileges.user_id = 5) A' at line 1: SELECT roles.* FROM roles  INNER JOIN privileges ON roles.id = privileges.role_id    WHERE ((privileges.user_id = 5) AND (clan_id = 5)) ) on line #12 of app/views/events/view.rhtml:
9:     <br>
10:     <%= simple_format(h(@event.info)) %>
11:
12:     <%= can_do_change %>
13: </div>
14: <div id="event_content">
15:

which relates to this code
def can_do_change
    role = Role.find_by_name("Matches")
    if (@event.tipe != "GameNight") && ((@logged_user.masters.include?(@clan.master)) || (@logged_user.leaders.include?(@event.squad)) || (@logged_user.roles_for_clan(@clan).include? role))
      render :partial => "match_status"
    else
      @event.status
    end
  end

I'm sure its the role = Role.find_by_name("Matches") but unsure why. any insights into this?

--

Re: deployment errors.

I don't see any mention of "Matches" in the SQL query, so I don't think that's the one causing the error. In fact, it looks like it's happening in the template, can you post the template code?

Railscasts - Free Ruby on Rails Screencasts

Re: deployment errors.

[code view]
<div id="event_header">
    <h2>
        <%= unless @event.tipe == "GameNight"; h(@event.squad.name); else; @event.name; end;%> <%= h(@event.versus) %>
        <br />
        <%= distance_of_time_in_words(Time.now, @event.happens) %> Away
    </h2>
   
    <%= @event.happens %>
    <br>
    <%= simple_format(h(@event.info)) %>

    <%= can_do_change %>
</div>
<div id="event_content">

<div id="left_view">
    <h4>Comments</h4>
    <div id="comment_list">
        <ul>
        <%= render :partial => "comments", :collection => @event.comments unless @event.comments == false %>
        <%= render :partial => "no_comment" if @event.allow_comments == false %>
        <%= render :partial => "comment_form" if @event.allow_comments == true %>
        </ul>
    </div>
</div>
[/code]
I only get this error on pages that have this method.

  def can_do_change
    role = Role.find_by_name("Matches")
    if (@event.tipe != "GameNight") && ((@logged_user.masters.include?(@clan.master)) || (@logged_user.leaders.include?(@event.squad)) || (@logged_user.roles_for_clan(@clan).include? role))
      render :partial => "match_status"
    else
      @event.status
    end
  end

or this method
def see_link(role)
    role = Role.find_by_name(role)
    if (@logged_user.masters.include? @clan.master) || (@logged_user.roles_for_clan(@clan).include? role)
      true
    else
      false
    end
  end

[code other error message]
ActionView::TemplateError (Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'privileges ON roles.id = privileges.role_id    WHERE ((privileges.user_id = 5) A' at line 1: SELECT roles.* FROM roles  INNER JOIN privileges ON roles.id = privileges.role_id    WHERE ((privileges.user_id = 5) AND (clan_id = 5)) ) on line #5 of app/views/hq/index.rhtml:
2: <br />
3:
4:
5: <%= link_to (image_tag("/images/button_new_match.png")), {:action => :new, :controller => :events}, :class => "new_match" if see_link("leader") %>
6:
7: <%= image_tag("/images/button_upcoming_matches.png") %>
8: <table class="match_table" cellspacing="0" cellpadding="0">
[/code]

--

Re: deployment errors.

the rest of the error

 /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract_adapter.rb:128:in `log'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/connection_adapters/mysql_adapter.rb:399:in `select'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/base.rb:427:in `find_by_sql'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/base.rb:997:in `find_every'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/base.rb:418:in `find'
    /home/12788/data/rubygems/gems/gems/activerecord-1.15.2/lib/active_record/associations/has_many_through_association.rb:35:in `find'
    #{RAILS_ROOT}/app/models/user.rb:59:in `roles_for_clan'
    #{RAILS_ROOT}/app/helpers/application_helper.rb:16:in `see_link'
    #{RAILS_ROOT}/app/views/hq/index.rhtml:5:in `_run_rhtml_47app47views47hq47index46rhtml'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_view/base.rb:326:in `send'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_view/base.rb:326:in `compile_and_render_template'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_view/base.rb:301:in `render_template'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_view/base.rb:260:in `render_file'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:806:in `render_file'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:711:in `render_with_no_layout'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/layout.rb:247:in `render_without_benchmark'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:50:in `render'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:50:in `render'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:1096:in `perform_action_without_filters'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:638:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:438:in `call'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:637:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:638:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:438:in `call'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:637:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:638:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:438:in `call'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:637:in `call_filter'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:in `perform_action'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `send'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `process_without_filters'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:in `process'
    /home/12788/data/rubygems/gems/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in `process'
    /home/12788/data/rubygems/gems/gems/rails-1.2.2/lib/dispatcher.rb:41:in `dispatch'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/rails.rb:78:in `process'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/rails.rb:76:in `synchronize'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/rails.rb:76:in `process'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:618:in `process_client'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:617:in `each'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:617:in `process_client'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:736:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:736:in `initialize'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:736:in `new'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:736:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:720:in `initialize'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:720:in `new'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel.rb:720:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/configurator.rb:271:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/configurator.rb:270:in `each'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/configurator.rb:270:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/bin/mongrel_rails:127:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/lib/mongrel/command.rb:211:in `run'
    /home/12788/data/rubygems/gems/gems/mongrel-1.0.1-i386-linux/bin/mongrel_rails:243
    /home/12788/data/rubygems/gems/bin/mongrel_rails:16:in `load'
    /home/12788/data/rubygems/gems/bin/mongrel_rails:16

--

Re: deployment errors.

Looks like it's happening inside "roles_for_clan" in user.rb. Can you post this?

Railscasts - Free Ruby on Rails Screencasts

Re: deployment errors.

[code roles_for_clan]
  def roles_for_clan(clan)
    self.roles.find(:all, :conditions => ["clan_id = ?", clan])
  end
[/code]

rest of user.rb

class User < ActiveRecord::Base
  has_and_belongs_to_many :clans
  has_many :settings
  has_many :events
  has_many :matches, :through => :rosters, :source => :event, :conditions => "happens >= NOW() and answer = 'yes'"
  has_many :members
  has_many :leaders, :through => :members, :source => :squad, :conditions => "position = 'leader'"
  has_many :squads, :through => :members
  has_many :invites
  has_many :requests
  has_many :rosters
  has_many :masters
  has_many :privileges
  has_many :roles, :through => :privileges
  has_many :comments
  has_many :posts
  has_many :topics
 
  #validates_presence_of :email, :password
  validates_presence_of :name
  validates_uniqueness_of :name
 
  attr_accessor :password_confirmation
  validates_confirmation_of :password
 
  def before_validation
    self.email.downcase!
    self.name = self.display_name.downcase
  end
 
  def validate
    errors.add_to_base("Missing password") if hashed_password.blank?
  end
  # Validations End
 
  def self.authenticate(name, password)
    name.downcase!
    user = self.find_by_name(name)
    if user
      expected_password = encrypted_password(password, user.salt)
      if user.hashed_password != expected_password
        user = nil
      end
    end
    user
  end
 
  def password
    @password
  end
 
  def password=(pwd)
    @password = pwd
    create_new_salt
    self.hashed_password = User.encrypted_password(self.password, self.salt)
  end

  def roles_for_clan(clan)
    self.roles.find(:all, :conditions => ["clan_id = ?", clan])
  end
 
  private
  def self.encrypted_password(password, salt)
    string_to_hash = password + "piggy" + salt
    Digest::SHA1.hexdigest(string_to_hash)
  end
 
  def create_new_salt
    self.salt = self.object_id.to_s + rand.to_s
  end
 
  def squad_ids=(squad_ids)
    members.each do |member|
      #member.update_attribute(:position, "basic")
      member.destroy unless squad_ids.include? member.squad
      #member.create(:squad_id => squad_ids, :position => "basic", :clan_id => 5) unless squad_ids.include? member.squad
    end
   
    squad_ids.each do |squad|
      #self.members.destroy unless blank?
    end
   
    squad_ids.each do |squad_id|
      self.members.create(:squad_id => squad_id, :position => "leader", :clan_id => 5)
    end
  end
 
  def roles=(roles)
  end
 
  def squads=(squads)
  end
 
end

--

Re: deployment errors.

Try going to your mysql database from the command line and entering this SQL.

SELECT roles.* FROM roles  INNER JOIN privileges ON roles.id = privileges.role_id    WHERE ((privileges.user_id = 5) AND (clan_id = 5))

MySQL says there's a syntax error in this statement, but I can't find it.

Railscasts - Free Ruby on Rails Screencasts

Re: deployment errors.

I had to do it through phpmyadmin, I've been having someproblems connecting through command line.

[code error]
Error
SQL query:

SELECT roles. *
FROM roles
INNER JOIN PRIVILEGES ON roles.id = privileges.role_id
WHERE (
(
privileges.user_id =5
)
AND (
clan_id =5
)
)
LIMIT 0 , 30

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'privileges ON roles . id = privileges . role_id WHERE ( ( privileges . user_id =' at line 1
[/code]

--

Re: deployment errors.

I should have check this earlier.

host mysql version 4.1.11
my mysql version 5.0.21

I dont know what the difference is tbh

--

Re: deployment errors.

fixed it. privileges is a reserved word in mysql 4 but not in 5

--