Rails 3 and SQLite communications concerning boolians

浪尽此生 提交于 2019-12-11 13:34:31

问题


im using Rails 3.2.8 and SQLite 1.3.6

and im running into troubles changing boolian variables in the database

SQLite translates "t" and "f" as true and false

this function wont update the :approved attribute (that is default false)

def activate
  @user = User.find(params[:id])

  if @user.update_attribute(:approved, 't')

    redirect_to "/" #this is the return i get so the update returns true

  else
   render "/users?approved=false"
  end 
end

But changing strings like @user.update_attribute(:email,'root@email.org') will update the email

how can i fix this do i need some kind of an sqlite adapter ?

i already installed activerecord-jdbcsqlite3-adapter and it messed up my app because i was too hasty and didnt notice that it was deprecated :P

i found this thread : Rails 3 SQLite3 Boolean false but i dont understand what i need to do in order to make Rails and SQLite communicate correctly as im a newbie in rails :)

also i think its worth mentioning that im running this on windows 7 x64

UPDATE

Apparently Rails does indeed know how to communicate with Sqlite, but the i have no idea why my code dosnt work for boolians lol

in my view i have :

<% @users.each do |user| %> 
<tr> 
  <td><%= user.id %></td> 
  <td><%= user.email %></td>
  <td><%= user.approved? %></td>
  <td>
<% if !user.approved? %>
  <%= link_to 'Approve', active_user_path(user), :method => :put %> 
    <% end %> </td> 
</tr> 
<% end %> 

This lists all the unapproved users and a link to activate them (set the bool to true)

and in my controller i have

    def activate
      @user = User.find(params[:id])

    if @user.update_attribute(:approved, true)

      redirect_to "/" #this is the return i get so the update returns true

    else
      render "/users?approved=false" #this is not rendered
    end 
  end

and my route

match "users/:id/activate" => "users#activate", :as => "active_user"

This works for other strings like the user name, address etc but not the bool's


回答1:


If the approved column on @user is a boolean, you should just pass true/false and let the database adaptor figure it out.

@user.update_attribute(:approved, true)



回答2:


I solved this by using a integer migration to my users table, i have no idea as of why @user.update_attribute(:approved, true) didnt save to the database it must have had something to do with my setup

i made the migration add_column :users, :is_approved, :integer, :default => 0 and when i want to activate a user i simply flip the integer value to 1



来源:https://stackoverflow.com/questions/13211811/rails-3-and-sqlite-communications-concerning-boolians

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!