class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
belongs_to :user
end
Nothing complex until now. When I play with that in the console:
Loading development environment (Rails 4.2.0)
irb(main):001:0> user = User.first
User Load (0.0ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "toto", created_at: "2015-01-16 11:18:41", updated_at: "2015-01-16 11:18:41">
irb(main):002:0> user.posts.size
(0.0ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 1]]
=> 0
irb(main):003:0> post = user.posts.build(title: 'ruby')
=> #<Post id: nil, user_id: 1, title: "ruby", created_at: nil, updated_at: nil>
irb(main):004:0> user.posts.size
=> 1
irb(main):005:0>
user1 = User.first
user.posts(force_reload: true).size
def create |
@operation = @account.operations.new(operation_params) |
if @operation.save |
redirect_to @account, notice: t(:created_success, model: Operation.model_name.human) |
else |
render "new" |
end |
end What I'd like to do is to check the sum to be withdrawn in the above operation so that the balance of the account not to be negative. That's how I |
Colin