eyssix
eyssix

Reputation: 27

Rails application using mysql - syntax error only with mysql not sqlite?

I'm very green to ruby on rails and have been working on getting an application working with mysql, everything works except for one particular page when loading assets from mysql database.

I'm not sure what is going on and have not had luck fixing it.

I'm running on ubuntu, with passenger 4.0.20, mysql 2.9.1 gem, rails 3.0.7,

when viewing the page either under the passenger daemon or through strictly apache the app returns this:

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 '== 'assets')' at line 1: SELECTtypes.* FROMtypesWHERE (type_for == 'assets')

Extracted source (around line #52):

49:   </div>
50:   <div class="field">
51:     <%= f.label :asset_type_id %><br />
52:     <%= f.select :asset_type_id, Type.where("type_for == 'assets'").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {}  %>    
53:   </div>
54:   <div class="field">
55:     <%= f.label :asset_status_id %><br />  

app/views/assets/_form.html.erb:52:in `block in_app_views_assets__form_html_erb___1678548858904611904_30514700_2370109183913626002'
app/views/assets/_form.html.erb:1:in         `_app_views_assets__form_html_erb___1678548858904611904_30514700_2370109183913626002'
app/views/assets/new.html.erb:3:in `_app_views_assets_new_html_erb___211939086717483877_30551560__3850816556217762359'

I'm at my end of possible things to try, not really sure where to go with this. Any help would be amazing right now.

Upvotes: 0

Views: 74

Answers (2)

Pavan
Pavan

Reputation: 33542

You have a syntax error on this line

<%= f.select :asset_type_id, Type.where("type_for == 'assets'").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {} %>

it should be like this

<%= f.select :asset_type_id, Type.where("type_for" => "assets").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {} %>

OR

you can write it as

<%= f.select :asset_type_id, Type.where("type_for = ?", "assets").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {} %>

OR

<%= f.select :asset_type_id, Type.where(:conditions =>["type_for = ?", "assets"]).collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {} %>

And As @MikeCampbell pointed,it can also written as

<%= f.select :asset_type_id, Type.where("type_for" :"assets").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {} %>

Upvotes: 1

Saurabh
Saurabh

Reputation: 73639

You have to remove == from line 52 and replace with just =

52:     <%= f.select :asset_type_id, Type.where("type_for = 'assets'").collect{|u| [ u.name, u.id ]} , { :include_blank => true }, {}  %>    

Upvotes: 1

Related Questions