has-many

Two has_many links between the same models

守給你的承諾、 提交于 2019-12-01 11:49:02
I have users which have products through a habtm link, which is working. I want to add a link between the user model and the product model, to keep track of the creator of that product (who doesn't always own the product, of course) But when I write in my user and product models a new link, the application screws up because I can't distinguish the creator of a product from the owner of (a lot of) products . Can you help me ? Here is my models : class Product < ActiveRecord::Base belongs_to :group has_and_belongs_to_many :authors has_and_belongs_to_many :users # THIS IS OK (with appart table)

Grails hasOne and hasMany same domain

非 Y 不嫁゛ 提交于 2019-12-01 01:50:20
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? Shaoxuan 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 back to Person. Per the documentation : Use a hasOne association to store the foreign key reference in

Order Players on the SUM of their association model

喜夏-厌秋 提交于 2019-11-30 21:20:28
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 time someone visits the generated listings. Question What is the best pattern to solve this? And how

Alternative method for proxy_owner in ActiveRecord

坚强是说给别人听的谎言 提交于 2019-11-30 17:53:20
问题 ActiveRecord proxy_owner is now deprecated and the explanation here is very vague on how to change it, so I'm not sure how to use it my case: http://apidock.com/rails/ActiveRecord/Associations/AssociationProxy Here is what I'm trying to do: class Library < ActiveRecord::Base has_many :books do def some_method proxy_owner.author end end end I get a warning when I run this code that proxy_owner is deprecated: DEPRECATION WARNING: Calling record.books.proxy_owner is deprecated. Please use record

find all parent records where all child records have a given value (but not just some child records)

断了今生、忘了曾经 提交于 2019-11-30 16:57:51
An event has many participants. A participant has a field of "status". class Event < ActiveRecord::Base has_many :participants end class Participant < ActiveRecord::Base belongs_to :event end I need to find all events except the following ones: events where every one of its participants has a status of 'present'. I can find all events where some of its participants have a status of 'present' with the following AR code: Event.joins(:participants).where .not(participants: {status: 'present'}) .select("events.id, count(*)") .group("participants.event_id") .having("count(*) > 0") That creates SQL

find all parent records where all child records have a given value (but not just some child records)

跟風遠走 提交于 2019-11-30 16:18:48
问题 An event has many participants. A participant has a field of "status". class Event < ActiveRecord::Base has_many :participants end class Participant < ActiveRecord::Base belongs_to :event end I need to find all events except the following ones: events where every one of its participants has a status of 'present'. I can find all events where some of its participants have a status of 'present' with the following AR code: Event.joins(:participants).where .not(participants: {status: 'present'})

rails map.resources with has_many :through doesn't work?

孤者浪人 提交于 2019-11-30 16:00:21
I've got three (relevant) models, specified like this: class User < ActiveRecord::Base has_many :posts has_many :comments has_many :comments_received, :through => :posts, :source => :comments end class Post < ActiveRecord::Base belongs_to :user has_many :comments end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end I'd like to be able to reference all the comments_received for a user with a route - let's say it's for batch approval of comments on all posts. (note that you can also get comments made by the user , but users can't comment on their own posts, so the

PostgreSQL, Rails and :order => problem

拜拜、爱过 提交于 2019-11-30 14:03:33
I have the following line in my ActiveRecord model: class Record < ActiveRecord::Base has_many :users, :through => :record_users, :uniq => true, :order => "record_users.index ASC" This is intended to enable me to read out record.users in a way that I order using an index field in the record_users model. The problem is that this fails on PostgreSQL with the following error: ActionView::TemplateError (PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list Is there a way to fix the statement to make it work? Casper Fabricius I suppose you could call it a bug in

Nested attributes for belongs_to association rails

久未见 提交于 2019-11-30 11:45:39
I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to :user, :class_name => 'User', :foreign_key => 'id' belongs_to :company, :class_name => 'Company', :foreign_key => 'id' has_many :replies accepts_nested_attributes_for :company end class Company < ActiveRecord::Base attr_accessible :name has_many :complaints, :class_name => 'Complaint', :foreign_key => 'id' has_many :branches, :class_name => 'Branch',

Rails - check if record exists in has_many association

℡╲_俬逩灬. 提交于 2019-11-30 11:31:48
I'm not sure if my question is worded correctly. I have three models: User , Item , and UserItem . user has_many :user_items user has_many :items, through :user_items item has_many :user_items item has_many :users -> {uniq}, through :user_items item belongs_to :user user_item belongs_to :user user_item belongs_to :item I need a way to see if a user has an item to make if statements in my item views But here's the catch, user_items have enum status: [ :pending, approved] . So I need to see if a current_user has a certain :pending item. For example when a user visits item1's view page I have the