Hi,
I am using this code from railscast for searching in my activerecords:
http://railscasts.com/episodes/37-simple-search-form
There is this method in the ''Product''-Model, which is
searching for
the product name:
class Project < ActiveRecord::Base
has_many :tasks
validates_presence_of :name
def self.search(search)
if search
find(:all, :conditions => [''name LIKE ?'',
"%#{search}%"]) #Rails
2
where(''name LIKE ?'', "%#{search}%") #Rails 3
else
find(:all)
end
end
end
My question is now, how can I search for products that have a given
task name. Task is a nested attribute for Product. I only use one of
the search line above. The second one is only possible with Rails 3.
I don''t find anything about nested queries for activerecords with
''find'' or ''where''.
I hope someone can help!
Sebastian
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 6, 2:57 pm, Sebastian <sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi, > > I am using this code from railscast for searching in my activerecords:http://railscasts.com/episodes/37-simple-search-form > > There is this method in the ''Product''-Model, which is searching for > the product name: > > class Project < ActiveRecord::Base > has_many :tasks > validates_presence_of :name > > def self.search(search) > if search > find(:all, :conditions => [''name LIKE ?'', "%#{search}%"]) #Rails > 2 > where(''name LIKE ?'', "%#{search}%") #Rails 3 > else > find(:all) > end > end > end > > My question is now, how can I search for products that have a given > task name. Task is a nested attribute for Product. I only use one of > the search line above. The second one is only possible with Rails 3. > > I don''t find anything about nested queries for activerecords with > ''find'' or ''where''. >You can do stuff like Project.joins(:tasks).where(:tasks => {:some_column_on_tasks = 23}) (in rails to that would be find :all, :joins => :tasks, :conditions => {:tasks => {...}} If you use the string form of conditions you just need to prepend the table name to the column name, ie where(''tasks.name like ...'') Fred> I hope someone can help! > Sebastian-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you so much, it''s working great! I also found that in the
official Rails 3 guides, but I didn''t know that I have to look for the
join method.
Now I am using the following code, just for case that anybody else
has the same issue:
class Project < ActiveRecord::Base
has_many :tasks
validates_presence_of :name
def self.search(search)
if search
joins(:tasks).where(''name LIKE ?'',
"%#{search}%") #Rails 3
else
find(:all)
end
end
end
On 6 Jul., 17:37, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Jul 6, 2:57 pm, Sebastian
<sebastian.go...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > I am using this code from railscast for searching in my
activerecords:http://railscasts.com/episodes/37-simple-search-form
>
> > There is this method in the ''Product''-Model, which
is searching for
> > the product name:
>
> > class Project < ActiveRecord::Base
> > has_many :tasks
> > validates_presence_of :name
>
> > def self.search(search)
> > if search
> > find(:all, :conditions => [''name LIKE ?'',
"%#{search}%"]) #Rails
> > 2
> > where(''name LIKE ?'', "%#{search}%")
#Rails 3
> > else
> > find(:all)
> > end
> > end
> > end
>
> > My question is now, how can I search for products that have a given
> > task name. Task is a nested attribute for Product. I only use one of
> > the search line above. The second one is only possible with Rails 3.
>
> > I don''t find anything about nested queries for activerecords
with
> > ''find'' or ''where''.
>
> You can do stuff like Project.joins(:tasks).where(:tasks =>
> {:some_column_on_tasks = 23}) (in rails to that would be
> find :all, :joins => :tasks, :conditions => {:tasks => {...}}
>
> If you use the string form of conditions you just need to prepend the
> table name to the column name, ie where(''tasks.name like
...'')
>
> Fred
>
>
>
>
>
>
>
> > I hope someone can help!
> > Sebastian
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.