Sunday 15 June 2014

ruby on rails - why is @followed.email undefined? -



ruby on rails - why is @followed.email undefined? -

i'm trying create friending scheme app.

i using active_relationships, passive_relationships, state_machine , concept of followers,followed, next , followers.

i have gotten myself in bit of knot comprehending how fit together. looking help finding clarity how utilize i've created access friend id's view friend profile pages, email friends etc.

to highlight kinds of things i'm struggling comprehend: @ moment i'm faced with:

test_should_send_request_email#relationshiptest nomethoderror: undefined method `email' app/mailers/user_mailer.`rb:38:in `relationship_requested' app/models/relationship.rb:11:in `send_request_email' test/models/relationship_test.rb:33:in `block (2 levels) in <class:relationshiptest>' test/models/relationship_test.rb:32:in `block in <class:relationshiptest>'

i have thought @followed.email valid method , cant work out why it's considered undefined. (user/follower) want email user (followed) send them relationship request. why can send email @followed.email?

model/user.rb:

class user < activerecord::base has_one :profile, dependent: :destroy has_many :active_relationships, class_name: "relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower

model/relationship.rb:

class relationship < activerecord::base belongs_to :follower, class_name: "user" belongs_to :followed, class_name: "user" validates :follower_id, presence: true validates :followed_id, presence: true state_machine :state, initial: :pending end def send_request_email usermailer.relationship_requested(id).deliver end end

user_mailer.rb:

def relationship_requested(relationship_id) relationship = relationship.find(relationship_id) @user = relationship.follower @followed = relationship.followed mail service to: @followed.email, subject: "#{@user.name} wants friend you" end end

relationship_requested.html.erb:

hi <%= @followed.name %>, <%= @user.name %> wants friend you.

db tables:

class createusers < activerecord::migration def alter create_table :users |t| t.string :name t.string :email t.timestamps end end end class createrelationships < activerecord::migration def alter create_table :relationships |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 class addstatetorelationships < activerecord::migration def alter add_column :relationships, :state, :string add_index :relationships, :state end end

relationship_test.rb:

require 'test_helper' class relationshiptest < activesupport::testcase # test "the truth" # assert true # end def setup @relationship = relationship.new(follower_id: 1, followed_id: 2) end test "should valid" assert @relationship.valid? end test "should require follower_id" @relationship.follower_id = nil assert_not @relationship.valid? end test "should require followed_id" @relationship.followed_id = nil assert_not @relationship.valid? end test "should state pending until other user can take or reject request" assert_equal 'pending', @relationship.state end test "should send request email" @relationship = relationship.create(follower_id: 1, followed_id: 2) assert_difference 'actionmailer::base.deliveries.size', 1 @relationship.send_request_email end end end

your test creating relationship record , passing in 2 ids follower , following. ids correspond user records? doesn't it, test creates no users (do have fixtures 2 uses?)

so follower , next associations nil, though relationship 'valid' (your validations check presence of foreign keys, not actual instances of users).

put debug info test ("puts" console or utilize pry pause execution of test , interrogated variables).

i'd recommend looking @ using factorygirl , faker gems generating test data.

ruby-on-rails email friend

No comments:

Post a Comment