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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -