dynamic-finders

Dynamic finders with Grails many-to-many relationship

大憨熊 提交于 2019-12-19 10:15:53
问题 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 name int age String job static hasMany = [groups : Group] static belongsTo = [org : Organization] } class Group { String groupName String code static hasMany = [members : User] } My problems are: 1. The above relationship require one class hold belongsTo

Dynamic finders with Grails many-to-many relationship

删除回忆录丶 提交于 2019-12-19 10:15:03
问题 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 name int age String job static hasMany = [groups : Group] static belongsTo = [org : Organization] } class Group { String groupName String code static hasMany = [members : User] } My problems are: 1. The above relationship require one class hold belongsTo

Rails “find_all_by” vs “.where”

*爱你&永不变心* 提交于 2019-12-17 16:04:21
问题 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 any advantage to this? It seems like .where is more common than find_all_by nowadays. 回答1: My opinion is that using .where is a better approach. When you use attribute based finders, you are going to have to tunnel through a method missing call and

Filtering a collection with offset in grails

谁都会走 提交于 2019-12-11 04:53:33
问题 I'm trying to filter a collection in grails with findAll so I only get the instances with a certain value in his field "estado". I have something like this: trabajos.findAll({it.estado.equals( "Pago")}) The problem is I dont know how to paginate the returned collection. I took a look at grails documentation and found this Book.findAll(Map queryParams, Closure whereCriteria) but when I try it trabajos.findAll([offset: 0], {it.estado.equals("Pago")}) I get the following exception No signature

Dynamic finders with Grails many-to-many relationship

ⅰ亾dé卋堺 提交于 2019-12-01 11:02:03
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 name int age String job static hasMany = [groups : Group] static belongsTo = [org : Organization] } class Group { String groupName String code static hasMany = [members : User] } My problems are: 1. The above relationship require one class hold belongsTo to be the "owner" of the relationship. In this context, the User belongs to the Group, but I do not

Rails “find_all_by” vs “.where”

南笙酒味 提交于 2019-11-27 21:08:21
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 any advantage to this? It seems like .where is more common than find_all_by nowadays. My opinion is that using .where is a better approach. When you use attribute based finders, you are going to have to tunnel through a method missing call and eventually define a class method, via class_eval , that returns your result. This is extra processing that you may

find_or_create_by in Rails 3 and updating for creating records

好久不见. 提交于 2019-11-27 18:53:23
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 are room_id, date, and source. The other 2 are price and spots available (these change hourly, daily etc.) My question is, should I first find or create the record, then update (or create) the price and spots or can I do it all at once? You can see the two ways I'm doing it now, and I'm not sure if its actually doing what I'm expecting. Also, is

find vs find_by vs where

人走茶凉 提交于 2019-11-27 02:51:54
I am new to rails. What I see that there are a lot of ways to find a record: find_by_<columnname>(<columnvalue>) find(:first, :conditions => { <columnname> => <columnvalue> } where(<columnname> => <columnvalue>).first And it looks like all of them end up generating exactly the same SQL. Also, I believe the same is true for finding multiple records: find_all_by_<columnname>(<columnvalue>) find(:all, :conditions => { <columnname> => <columnvalue> } where(<columnname> => <columnvalue>) Is there a rule of thumb or recommendation on which one to use? Use whichever one you feel suits your needs best

find_or_create_by in Rails 3 and updating for creating records

孤人 提交于 2019-11-26 19:39:45
问题 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 are room_id, date, and source. The other 2 are price and spots available (these change hourly, daily etc.) My question is, should I first find or create the record, then update (or create) the price and spots or can I do it all at once? You can see

find vs find_by vs where

ε祈祈猫儿з 提交于 2019-11-26 10:10:37
问题 I am new to rails. What I see that there are a lot of ways to find a record: find_by_<columnname>(<columnvalue>) find(:first, :conditions => { <columnname> => <columnvalue> } where(<columnname> => <columnvalue>).first And it looks like all of them end up generating exactly the same SQL. Also, I believe the same is true for finding multiple records: find_all_by_<columnname>(<columnvalue>) find(:all, :conditions => { <columnname> => <columnvalue> } where(<columnname> => <columnvalue>) Is there