Ruby on Rails Tutorial (20)

このところ Rails ヘビローですがそのまま突き進みたいと思っています。ラストの chapter 11 です。
とりあえず、いつものように branch 作成とのこと。

$ git checkout -b following-users

この章では social な機能実装とのこと。ユーザ間の model の関連が云々、とか書いてありますね。他にも色々と面白そうな事が書いてありますが、とっととヤッツケて再読 (というか解析)しつつ云々に早めにとりかかる方向で。

11.1 The Relationship model

とりあえず model から云々とのこと。基本的な relationship として

  • user has_many followed users
  • user has_many followers

なんだけど色々ある模様。

11.1.1 A problem with the data model (and a solution)

これは昔で言う habtm というヤツですね。has_many :thruough か。詳細スルーでどんどん進めます。

$ rails generate model Relationship follower_id:integer followed_id:integer
      invoke  active_record
      create    db/migrate/20130130105206_create_relationships.rb
      create    app/models/relationship.rb
      invoke    rspec
      create      spec/models/relationship_spec.rb

で、migrate なソレを以下に。

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps
    end

    add_index :relationships, :follower_id
    add_index :relationships, :followed_id
    add_index :relationships, [:follower_id, :followed_id], unique: true
  end
end

で、以下か。

$ bundle exec rake db:migrate
==  CreateRelationships: migrating ============================================
-- create_table(:relationships)
   -> 0.3246s
-- add_index(:relationships, :follower_id)
   -> 0.0008s
-- add_index(:relationships, :followed_id)
   -> 0.0008s
-- add_index(:relationships, [:follower_id, :followed_id], {:unique=>true})
   -> 1.3131s
==  CreateRelationships: migrated (1.6399s) ===================================

$ bundle exec rake db:test:prepare

このまま次にすすむ

11.1.2 User/relationship associations

そういえば試験を書いていなかったので何もやりようが無いのか。以下の試験を云々とあります。
spec/models/relationship_spec.rb

require 'spec_helper'

describe Relationship do

  let(:follower) { FactoryGirl.create(:user) }
  let(:followed) { FactoryGirl.create(:user) }
  let(:relationship) { follower.relationships.build(followed_id: followed.id) }

  subject { relationship }

  it { should be_valid }

  describe "accessible attributes" do
    it "should not allow access to follower_id" do
      expect do
        Relationship.new(follower_id: follower.id)
      end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end
  end
end

ええと application.rb で云々とありますね。今はまだ色々と微妙らしい。そして user な model にも以下な属性が追加された模様で Listing 11.3 な試験が追加されてます。
で、Rails の名前で決まるソレで微妙な不具合が出てくるとのことで app/models/user.rb を以下に、とのこと。

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :admin
  has_secure_password
  has_many :microposts, dependent: :destroy
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy

あと、spec/models/relationship_spec.rb に以下を追加らしい。

  describe "follower methods" do
    it { should respond_to(:follower) }
    it { should respond_to(:followed) }
    its(:follower) { should == follower }
    its(:followed) { should == followed }
  end

う、ヤバい。そろそろ限界かも。Listing 11.6 のあたりは若干微妙。これを盛り込んで試験 green 確認はしてます。

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id, :follower_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
end

と思ったら

red 終了。app/models/relationship.rb が以下のままでした。

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id, :follower_id

11.3 節以降は明日ってことで。