Scope to fetch Associated data from Database Rails 3 -


basically wanted use scope fetch data using association.

association between user , posts.

 belongs_to :user # on posts model  has_many :posts, :dependent => :destroy #on user model 

query

database has number records of posts created various users, wanted fetch posts post.user.name doesn't have pattern (integer etc) in it.

i wish use

post.scope_name 

i did this

scope :posts_by, joins("left outer join users u on u.id = posts.user_id").where('users.name regexp ? ', "my_pattern") 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -