Skip to content

Instantly share code, notes, and snippets.

class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: ‘User’
belongs_to :recipient, :foreign_key => :recipient_id, class_name: ‘User’
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
where(“(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)”, sender_id,recipient_id, recipient_id, sender_id)
end
end
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.text :body
t.references :conversation, index: true
t.references :user, index: true
t.boolean :read, :default => false
end
end
end
mfifth:~/workspace (master) $ bundle exec rspec
.....F......
Failures:
1) Users can send messages to each other with valid attributes.
Failure/Error: click_link admin.email
Capybara::ElementNotFound:
Unable to find link "[email protected]"
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.blank? ? '' : created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: "User"
belongs_to :recipient, :foreign_key => :recipient_id, class_name: "User"
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id, recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id
From: /home/ubuntu/workspace/app/controllers/messages_controller.rb @ line 48 MessagesController#set_conversation:
47: def set_conversation
=> 48: binding.pry
49: @conversation = Conversation.find(params[:conversation_id])
50: end
[1] pry(#<MessagesController>)> params
=> {"controller"=>"messages", "action"=>"index", "conversation_id"=>"#<Conversation::ActiveRecord_Relation:0x007f502caca4f8>"}
[2] pry(#<MessagesController>)>
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :set_recipient, only: [:create]
def index
@conversations = Conversation.all
@conversation = Conversation.new
end
def create
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.references :user, index: true, foreign_key: true
t.references :shirt, index: true, foreign_key: true
t.timestamps null: false
end
end
end
class CreateOrders < ActiveRecord::Migration
def change
create_table :orders do |t|
t.references :user, index: true, foreign_key: true
t.references :shirt, index: true, foreign_key: true
t.timestamps null: false
end
end
end
@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";
@import "shirts";
body {
margin-left: 10px;
margin-top: 60px;
border: solid 5px black;
}