Questions tagged [dynamic-finders]

49 questions
177
votes
13 answers

find vs find_by vs where

I am new to rails. What I see that there are a lot of ways to find a record: find_by_() find(:first, :conditions => { => } where( => ).first And it looks like all of them…
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
29
votes
2 answers

Rails "find_all_by" vs ".where"

I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing it to: def maturities InfoItem.where(work_order: self.work_order).map(&:maturity) end Would there be…
ardavis
  • 9,842
  • 12
  • 58
  • 112
19
votes
2 answers

Rails find_or_create_by where block runs in the find case?

The ActiveRecord find_or_create_by dynamic finder method allows me to specify a block. The documentation isn't clear on this, but it seems that the block only runs in the create case, and not in the find case. In other words, if the record is found,…
Simon Woodside
  • 7,175
  • 5
  • 50
  • 66
9
votes
3 answers

find_or_create_by in Rails 3 and updating for creating records

I'm not sure if I should be updating records this way or if I'm missing something. I have a table with 5 columns (not including timestamps and id) 3 of which are distinct, and 2 which will get updated. The 3 distinct which I will find or create by…
holden
  • 13,471
  • 22
  • 98
  • 160
5
votes
1 answer

How to write a grails dynamic finder looking for 0 or null

What's the best way to write a Grails dynamic finder that is searching for a flag that equals 0 or is null? Example class Book { String title Date releaseDate String author Boolean paperback Integer isArchived } isArchived is…
anataliocs
  • 10,427
  • 6
  • 56
  • 72
5
votes
1 answer

Does findOrCreateBy work with other domain instances?

I'm trying to use findOrCreateBy to search for an object or instantiate one if I can't find one that matches, but it isn't working as I expected. This is what I have: String myBaz = "some unique string" FooType myFooType =…
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
3
votes
2 answers

Dynamic finders with Grails many-to-many relationship

I have 2 domain classes which are mapped by many-to-many relationship. I followed the instruction of Grails documentation, but I still have some problem when processing data on those domains. Here are my 2 domain classes: class User { String…
Đinh Hồng Châu
  • 5,300
  • 15
  • 53
  • 90
3
votes
1 answer

How to override a grails dynamic finder?

Say I have a class Person with a name field. I can do a Person.findByName but what if I want to override this method to ensure that the query is cached? How do I override this dynamic finder method?
Champ
  • 1,291
  • 4
  • 16
  • 32
3
votes
1 answer

Rails dynamic finders occasionally do not work

I have a problem where the dynamic finders used in my Ruby on Rails application almost always work, but very rarely a NoMethodError will be thrown. Here is a copy of the exception stack from when things do go wrong: 2013-10-16 05:34:04.723…
Gary
  • 61
  • 2
2
votes
1 answer

Dynamic attribute-based finders with Active Record RoR, Model.find_by_ does not work

I am a newby with RoR, Environment : Rails 3.09 and ruby 1.9.2p290 installed on leopard I am following carefully the tutorial of Michael hartl (http://ruby.railstutorial.org/). At the chapter 7, with the console we should test this code : $ rails…
2
votes
1 answer

How do I avoid nil with a dynamic finder when an attribute comes as nil?

link_to 'articles', articles_path, :attr1 => 'foo', :attr2 => 'bar' And in the controller: Article.find_all_by_attr1_and_attr2(params[:attr1], params[:attr2]) However if the controller receives only [:attr1] I get a nil.
elkalto23
  • 267
  • 3
  • 12
2
votes
4 answers

Using dynamic finders to specify NOT NULL

I very often want to use dynamic finders to specify NOT NULL. So… this works: Widget.find_all_by_color('blue') this works: Widget.find_all_by_color(nil) But how can I do SELECT * FROM `widgets` WHERE `color` IS NOT NULL; ?
John Bachir
  • 22,495
  • 29
  • 154
  • 227
2
votes
2 answers

Grails domain class initialization

My Grails app has the following Spring bean defined in spring/resources.groovy calendarService(CalendarService) { bean -> bean.initMethod = "init" } This method looks something like: class CalendarService { void init() { …
Dónal
  • 185,044
  • 174
  • 569
  • 824
2
votes
2 answers

Is it possible to restrict dynamic finders?

Imagine that you have a Grails projects with a great service layer and you want to make sure that your team only accesses the model through your services. Is it easily possible to restrict the dynamic finders so that they can be still used from…
rdmueller
  • 10,742
  • 10
  • 69
  • 126
2
votes
0 answers

Grails Unit Test - test a service which uses a findAllBy* method

We have Grails 2.2.1 on a project. I want to test a service. In this service I use a dynamicFinder (findAllBy...) but the result of this finder when used in a unit test using domain mocks is an EmptyList. But when I look in UserRole.list() there are…
Sandra
  • 56
  • 3
1
2 3 4