has-many

How can I find records by “count” of association using rails and mongoid?

只愿长相守 提交于 2019-12-20 11:09:49
问题 With these models: class Week has_many :proofs end class Proof belongs_to :week end I want to do something like: Week.where(:proof.count.gt => 0) To find only weeks that have multiple proofs. There is one answer that seems to address this: Can rails scopes filter on the number of associated classes for a given field But in this example, there is no such attribute as proof_ids in Week since the ids are stored with the proofs. This does not work for example: Week.where(:proof_ids.gt => 0) How

How to display highest rated albums through a has_many reviews relationship

霸气de小男生 提交于 2019-12-20 06:36:10
问题 I'm setting up a simple ruby/rails app where users can review albums. On an album's show page I average all the user reviews associated with that album through this code in my albums controller def show @album = Album.find_by_id(params[:id]) if @album.reviews.present? @ratings = Review.where(album_id: @album).average(:rating).truncate(2) else render 'show' end end This gives me an average rating for each album. On my home page (routed through a different controller) I want to display the top

CakePHP increment value

别来无恙 提交于 2019-12-20 04:07:17
问题 My problem looks as follows: I want to make a vote app where People can choose one or more Events(like Doodle). For this I have set up a function called vote. In the View you can choose the Events using checkboxes. The Models are Poll and Groupevent. A Poll has Many Groupevents. My Problem is when I call updateAll() , all values of the associated Groupevents are incremented. Here is my code: View: echo $this->Form->create('Poll', array('action' => 'vote')); for($i=0; $i<$count; $i++){ echo

Grails hasOne and hasMany same domain

痴心易碎 提交于 2019-12-19 04:56:16
问题 I have domain like this: class Team { hasOne [leader: Person] hasMany [member: Person] } class Person { belongsTo [team: Team] } But when the tables are generated, there is not a column like leader_id in the team table. And thus the leader relation is not persisted. How should I fix it? 回答1: I figured that, what I need is class Team { belongsTo [leader: Person] hasMany [member: Person] } class Person { belongsTo [team: Team] } so that the Team table can have the desired "leader" reference

Order Players on the SUM of their association model

巧了我就是萌 提交于 2019-12-19 03:25:12
问题 I have a database with 6500 players and each player has an average of 15 game results . Use case I want to generate a list of players, ordered by the sum of their prize money (a field in the results table). I prefer this to be in some sort of scope, so I can also filter the list on the player's country, etc. Performance I have seen posts that mention a cache_counter field for performance. In my case I have thousands of result records (75.000+) so I don't want the calculations being done every

RAILS: How to get has_many associations of a model

南笙酒味 提交于 2019-12-17 19:13:13
问题 how I can get the has_many associations of a model? For example if I have this class: class A < ActiveRecord::Base has_many B has_many C end I would a method like this: A.get_has_many that return [B,C] Is it possible? Thanks! 回答1: You should be using ActiveRecord reflections. Then you can type something like this: A.reflect_on_all_associations.map { |assoc| assoc.name} which will return your array [:B, :C] 回答2: For Example you could try : aux=Array.new Page.reflections.each { |key, value| aux

Rails find record with zero has_many records associated [duplicate]

独自空忆成欢 提交于 2019-12-17 08:04:12
问题 This question already has answers here : Want to find records with no associated records in Rails (8 answers) Closed 5 months ago . This seems fairly simple but I can't get it to turn up on Google. If I have: class City < ActiveRecord::Base has_many :photos end class Photo < ActiveRecord::Base belongs_to :city end I want to find all cities that have no photos. I'd love to be able to call something like... City.where( photos.empty? ) ...but that doesn't exist. So, how do you do this kind of

Rails has_many :through Find by Extra Attributes in Join Model

橙三吉。 提交于 2019-12-17 03:46:46
问题 New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user #For clarity's sake, EventUser also has a boolean column "active", among others end class Event < ActiveRecord::Base has_many :event_users has_many :users,

Rails3 ActiveRecord - Fetching all records that have A and B (through a has_many relationship)

醉酒当歌 提交于 2019-12-14 02:38:55
问题 I have the following model relationships: class Article < ActiveRecord::Base has_many :tags, :through => :article_tags end class ArticleTag < ActiveRecord::Base belongs_to :tag belongs_to :article end class Tag < ActiveRecord::Base has_many :articles, :through => :article_tags end Now I want to load all the Articles that are tagged with both tag "A" and tag "B". I'm pretty new to Rails and it has been a few years since I did any serious web-dev/SQL work but for the life of me I can't figure

Read uncomitted data from HasMany-relationship with NHibernate

半腔热情 提交于 2019-12-13 07:08:22
问题 I have an NHibernate map which defines a HasMany-relationship on a type, i.e. a class has a list of another class. I would like NHibernate to be able to read uncommitted data including the list resulting from the HasMany-relationship. I have isolationlevel ReadUncomitted and I am able to write data and read it back before committing. However, the list is always empty, unless I commit first. Is there a way to make NHibernate populate objects with data from HasMany-relationships? EDIT It turns