# Modeling and querry difference?

**URL:** <https://www.ruby-forum.com/t/modeling-and-querry-difference/146443>\
**Category:** Rails\
**Created:** [August 21, 2008, 6:43am UTC](https://www.ruby-forum.com/t/modeling-and-querry-difference/146443 "2008-08-21T06:43:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 21, 2008, 6:43am UTC](https://www.ruby-forum.com/t/modeling-and-querry-difference/146443/1 "2008-08-21T06:43:47Z")

</div>

HI,  
I have this type of code where I used a modeling like  
‘user.profile\_photo’ from user object I am trying to find profile  
photo.but it is happened in a loop so just I want to know that how  
modeling works ? Internally it is a query if so then executing such  
thing inside a loop is a expensive. so when to use query and when to use  
modeling any feasible solution for a such a problem.

e.g:-  
@group=Group.find(group\_id)  
group\_users=@group.users.invitation\_accepted  
for user in group\_users  
if !@profile\_user.include?(user)  
if !user.profile\_photo.nil?  
@profile\_user \<\< user  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 21, 2008, 8:50am UTC](https://www.ruby-forum.com/t/modeling-and-querry-difference/146443/2 "2008-08-21T08:50:42Z")

</div>

hello,

@users = User.invitation\_accepted  
@users.each { |user| @profile\_user \<\< user if (@profile\_user.include?  
(user) and user.profile\_photo.nil?) }

in your model:

named\_scope :grouped, lambda { |id| { :include =\> :groups, :conditions  
=\> “groups.id = #{id} and groups.user\_id = user.id” } }

def invitation\_accepted  
User.grouped.find …  
end

untested

On 21 Aug., 06:43, Sunny B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 21, 2008, 9:21am UTC](https://www.ruby-forum.com/t/modeling-and-querry-difference/146443/3 "2008-08-21T09:21:08Z")

</div>

what is named\_scope and how it is useful in a performance perspective?

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 21, 2008, 9:56am UTC](https://www.ruby-forum.com/t/modeling-and-querry-difference/146443/4 "2008-08-21T09:56:52Z")

</div>

named\_scope is a smart way to chain model find options with synonyms  
but here it isn’t responsible for the query optimization

look at :include  
it iniate a join with group  
so your 2 queries are reduced to one query  
look at your development.log to see, what activerecord is building for  
you  
and if your query looks good

hint: look for some basic active records tutorial, espacially for  
eager loading and lazy loading

best regards

On 21 Aug., 09:21, Sunny B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
