Sha256: c35cc8c9fd55628e5f17327ee4a790df319d77c32f69d01d30d69474188c3785

Contents?: true

Size: 970 Bytes

Versions: 6

Compression:

Stored size: 970 Bytes

Contents

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user

  calculated :comments_by_user, ->(user) { "select count(*) from comments where comments.post_id = posts.id and comments.user_id = #{user.id}" }
  calculated :comments_count, -> { 'select count(*) from comments where comments.post_id = posts.id' }
  calculated :comments_two, -> { 'select count(*) from comments where comments.post_id = posts.id' }
  calculated :comments_arel, -> { Comment.where(Comment.arel_table[:post_id].eq(Post.arel_table[:id])).select(Arel.sql('count(*)')) }
  calculated :comments_to_sql, -> { Comment.where('comments.post_id = posts.id').select('count(*)') }
end

class Tutorial < Post
end

class Article < Post
  calculated :sub_comments, -> { 'select count(*) from comments where comments.post_id = posts.id' }
end

class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :comments
  has_many :posts
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
calculated_attributes-0.5.0 spec/support/models.rb
calculated_attributes-0.4.0 spec/support/models.rb
calculated_attributes-0.3.0 spec/support/models.rb
calculated_attributes-0.2.0 spec/support/models.rb
calculated_attributes-0.1.5 spec/support/models.rb
calculated_attributes-0.1.4 spec/support/models.rb