attr_accessor

If you declare an attr_accessor then you can use it as a virtual attribute, which is basically an attribute on the model that isn’t persisted to the database.

Example case: you declare attr_accessor :password in your User model so that you can use it as a field in a new user form. When you receive their password in the corresponding create action, you can derive a hashed_password, persist it to the database, and discard the given password (which is done automatically at the end of the request).


attr_accessor is a core feature of Ruby and is used to generate instance variables with getter and setter methods. Its use is never required in basic Ruby (it’s a convenience).

In the case of ActiveRecord models, getters and setters are already generated by ActiveRecord for your data columns. attr_accessor is not needed or desirable.

If you have additional instance data you don’t need to persist (i.e. it’s not a database column), you could then use attr_accessor to save yourself a few lines of code.

The similarly-named attr_accessible — which is frequently seen in Rails code and confused with attr_accessor — is a deprecated method of controlling mass assignment within ActiveRecord models. Rails 4 doesn’t support it out of the box; it has been replaced by Strong Parameters, which allows more granular control.

Source: http://stackoverflow.com/questions/20533925/why-is-attr-accessor-necessary-in-rails-4

NoMethodError: undefined method `password_digest=

I kept coming across this error when I ran the rails console and tried to set a user password:

NoMethodError: undefined method `password_digest=

It wasn’t written in the guide, but to solve this problem, you have to run a migration and create a new database which I called AddPasswordDigestToUsers (following Hartl’s code on Github).

$ rails generate migration AddPasswordDigestToUsers password_digest: string

Then a new file will be created with the following code written in it:

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end

Michael Hartl’s code on Github

Now, you should be able to run the $rails console and add a password to your user.

$rails console
>> User.first
>>user = User.first
>>user.password = “fresh”

Chapter 7 | Hartl | ActiveRecord::PendingMigrationError

Error! I’m on Chapter 7 of Michael Hartl’s tutorial and came across this error when I ran the rails server to view my app:

~workspace/sample_app (signup) $ bundle exec rake db:migrate RAILS_ENV=development
== 20150709195838 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "email" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /usr/local/rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `initialize'

This is the error that showed up on the main page of the web application:

raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)

Having no idea what this meant, I naturally did a search and found this answer on Stack Overflow that solved my problem.

I received a similar error when running rake:db migrate. To resolve my issue I ran rake:db drop to drop my database since I was in dev mode with no production database. Then I recreate the database with rake db:create after which i ran rake db:migrate successfully.

Error running rake db:migrate ActiveRecord::PendingMigrationError Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.

Resolved using:

rake db:drop - this will wipe the data out of your database rake db:create rake db:migrate

So I followed the instructions and ran ‘rake db:drop’ to clear my database. Then ran ‘rake db:create’ followed by ‘rake db:migrate’ and ‘finally $bundle exec rake db:migrate RAILS_ENV=production’ and my web app worked!

Unnamed QQ Screenshot20150721005531

XAMPP

I am trying to play around with WordPress offline and need Xampp to do that. After installing, my computer had a problem connecting to port 80 and I wasn’t sure why. My Skype was deactivated and my antivirus as well, but I still kept getting an error. Doing a search, I found this solution:

-> Clicked button Config from xammp.
1) I edited the file Apache (httpd.conf) with notepad
2) searched for this line: Listen 80
3) changed for this: Listen 8081
4) saved file
-> Clicked button Config from xammp.
1) I edited the file Apache (httpd-ssl.conf) with notepad
2) searched for this line: Listen 443
3) changed for this: Listen 444
4) saved file
I can run xammp from port 8081
http://localhost:8081/

Source: http://stackoverflow.com/questions/24097498/error-running-apache-after-xampp-install

LAMP Camp – FREE Developer Program

I applied to LAMP Camp today. Not sure if I will actually do it if accepted, but it seemed like a great deal.

“LAMP Camp is a fully-sponsored developer education program from the Cresca Group’s IT & Development Team that teaches people to build enterprise-level applications with PHP, MySQL, Javascript and much more.”

It’s a free program that starts any Monday you are ready and goes for 12 weeks. I’m learning Ruby on Rails at the moment and am unsure if I should pursue a PHP course, but they offer you a certificate of completion and you work in a small team, so it’s not a typical classroom setting. 12 weeks just means that I will still have to be unemployed and if I start this program in August, I’ll be unemployed through November. If they guaranteed a job after the program, and if I could eventually work remotely from anywhere in the world, it would be worth it.

Will have to do more research and learn the basics of PHP before I can attend.

Anyway, we’ll see if I get accepted.

Resetting the Migration

$ bundle exec rake db:migrate

While working on Chapter 6 of the Michael Hartl Ruby on Rails tutorial, I kept getting a crazy long error. This is an example of that error:

== 20141129134103 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'users' already exists: CREATE TABLE `users` (`id` int(11) auto_increment PRIMARY KEY, `name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
...
/home/barry/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rake-10.4.0/bin/rake:33:in `<top (required)>'
/home/barry/.rbenv/versions/2.1.2/bin/rake:23:in `load'
/home/barry/.rbenv/versions/2.1.2/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

The error was occurring because I had already created a table named Users. To solve this, I wanted to reset my database back to the original. I used

$ rake db:reset

and then ran

$ bundle exec rake db:migrate

and everything worked perfectly!


http://stackoverflow.com/questions/27202970/running-rake-dbmigrate-in-rails-fails-table-already-exists-using-mysql

rake db:migrate:down  # To have rails remove the table and keep in sync*

delete ('drop table users') the table in mysql and then re-run the migrations.

Use rake db:reset as indicated by jykim to drop the database.

Coder Manual

Today is day 2 of the Coder Manual lessons. I ended up paying the $39 price from Stacksocial for the online CoderManual course by Rob Dey, which was originally $399. At $39 and with lifetime access to the material, I didn’t think it was a bad investment.

I completed Phase 1 and Part 1 yesterday. Today I will be doing the Part 2 videos. So far the videos are pretty easy to follow and not too long so as to lose focus. Rob explains things in an easy to understand manner and it’s been great following along. As great as that sounds, Phase 1 was just an intro to computers and the internet, and Part 1 was just making sure you had the necessary software installed, so it wasn’t much in-depth coding. We’ll see how Part 2 is today.

Will keep you posted!

Fatal: ‘modeling-users’ does not appear to be a git repository

WOW, I’ve spent way too many hours trying to figure out why I was getting this error message when trying to push my changes to the ‘filling-in-layout’ branch of sample_app project.

sunsplat@rails-tutorial:~/workspace/sample_app (filling-in-layout) $ git push origin filling-in-layout
fatal: 'modeling-users' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

After reading countless posts on this subject from various resources, this link was the most helpful: http://stackoverflow.com/questions/17307154/git-bash-push-to-bitbucket-ignores-ssh-key

Fatal: error

I went into the ‘.git’ folder in my sample_app project to see what was going on. For some reason, in the /.git/config file, my remote ‘origin’ URL was set to ‘modeling-users’ when it should have been set to the Bitbucket URL for the project. I’ve changed it accordingly and it has solved the problem.

[remote "origin"]
url = git@bitbucket.org:sunsplat/sample_app.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

This is a good git command to remember:

git remote add origin URL_TO_YOUR_REPO