1

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

Community
  • 1
  • 1
Matti
  • 495
  • 6
  • 21
  • `SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).` Found this soon after i posted this, but isnt there somekind of adaptors that let me use treu and false with sqlite ? – Matti Nov 03 '12 at 17:58

2 Answers2

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)
Jason Noble
  • 3,756
  • 19
  • 21
  • i dont think the adaptor came with my sqlite gem, i also had to manually get the sqlite exe & dll files from their website – Matti Nov 03 '12 at 21:41
  • Rails has a SQLite adaptor included with it, that's how it talks to your database. Did you try passing `true` instead of `t`? – Jason Noble Nov 04 '12 at 01:23
  • Yeah ive tried calling the update with `@user.update_attribute(:approved, true)` but still it wont save it to the database – Matti Nov 04 '12 at 14:26
  • but the update returns true, as if it was successful but still dosnt save the new value – Matti Nov 04 '12 at 14:29
  • There's got to be something else wrong. It's working correctly for me: https://gist.github.com/4012352 – Jason Noble Nov 04 '12 at 15:48
  • Thanks for making the test but could you show me what gems are in the bundle ? – Matti Nov 04 '12 at 20:53
  • ill update my post so maybe you can help me spot why its acting like that (the update method works for everything except the boolians) :) – Matti Nov 04 '12 at 21:32
  • Did you update your activate method so it passes true/false? i.e. `@user.update_attribute(:approved, true)` – Jason Noble Nov 05 '12 at 06:24
  • yeah i did but the function wont update the boolian, in the update i copy/pasted my orginal post and just forgot to change the 't', sorry about that – Matti Nov 05 '12 at 10:43
0

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

Matti
  • 495
  • 6
  • 21