5. Extras
@jlucasps
Criar a action BillsController#create_bill
def create_bill
@bill = Bill.new(:user_id => params[:user_id], :name =>
params[:name])
@bill.description = params[:description]
@bill.date = params[:date]
@bill.value = params[:value]
if @bill.save
redirect_to @bill.user
else
@users = User.all
render :new
end
end
6. Extras
@jlucasps
Criar uma migration para a tabela comments
class CreateTableComments < ActiveRecord::
Migration
def change
create_table :comments do |t|
t.references :bill, :foreign_key => true
t.column :content, :string, :null => false
t.timestamps
end
end
end
9. Extras
@jlucasps
Alterar model Bill para adicionar relacionamento
class Bill < ActiveRecord::Base
# Attrs accessible
attr_accessible :name, :description, :user_id, :date, :value
# Validations
validates :name, :presence => true, :allow_blank => false
validates :user_id, :presence => true
validates :date, :presence => true
validates :value, :presence => true
# Associations
belongs_to :user
has_many :comments
...
end
10. Extras
@jlucasps
irb(main):002:0> bill = Bill.first
Bill Load (0.5ms) SELECT "bills".* FROM "bills" ORDER BY bills.date
DESC LIMIT 1
=> #<Bill id: 15, name: "123", description: "45", user_id: 9, date:
"2013-06-12 00:00:00", value: #<BigDecimal:3bc4e60,'0.124E3',9(36)
>, created_at: "2013-06-19 01:17:36", updated_at: "2013-06-19 01:17:
36">
irb(main):003:0> comment = Comment.new :bill_id => bill.id, :
content => "Conteúdo do comentário"
=> #<Comment id: nil, bill_id: 15, content: "Conteúdo do comentário",
created_at: nil, updated_at: nil>
irb(main):004:0> comment.save
(0.1ms) begin transaction
SQL (43.4ms) INSERT INTO "comments" ("bill_id", "content",
"created_at", "updated_at") VALUES (?, ?, ?, ?) [["bill_id", 15],
["content", "Conteúdo do comentário"], ["created_at", Tue, 25 Jun 2013
14:30:13 UTC +00:00], ["updated_at", Tue, 25 Jun 2013 14:30:13 UTC
+00:00]]
(393.4ms) commit transaction
11. Extras
@jlucasps
irb(main):005:0> bill.comments
Comment Load (0.4ms) SELECT "comments".* FROM "comments"
WHERE "comments"."bill_id" = 15 ORDER BY comments.created_at
DESC
=> [#<Comment id: 1, bill_id: 15, content: "Conteúdo do comentário",
created_at: "2013-06-25 14:30:13", updated_at: "2013-06-25 14:30:
13">]
irb(main):006:0> bill.comments.count
(0.3ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."
bill_id" = 15
=> 1
irb(main):007:0> comment.bill
Bill Load (0.4ms) SELECT "bills".* FROM "bills" WHERE "bills"."id" = 15
ORDER BY bills.date DESC LIMIT 1
=> #<Bill id: 15, name: "123", description: "45", user_id: 9, date:
"2013-06-12 00:00:00", value: #<BigDecimal:42d7f68,'0.124E3',9(36)
>, created_at: "2013-06-19 01:17:36", updated_at: "2013-06-19 01:
17:36">
irb(main):008:0>
12. Extras
@jlucasps
E se quisermos adicionar comentários em outras entidades ?
jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration
ChangeCommentsTable
invoke active_record
create db/migrate/20130625143912_change_comments_table.rb
class ChangeCommentsTable < ActiveRecord::Migration
def up
add_column :comments, :commentable_id, :integer
add_column :comments, :commentable_type, :string
remove_column :comments, :bill_id
end
def down
remove_column :comments, :commentable_id
remove_column :comments, :commentable_type
add_column :comments, :bill_id, :integer
end
end