has-many

has_many conditions or proc on foreign key

耗尽温柔 提交于 2020-01-14 18:13:51
问题 I have a has_many association between two models using a date as both the foreign and primary key for each model. It works perfectly one way but not the other. Works has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at Doesn't work has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at The reason being (i think) because the start_at on QuizLog is a date and the start_at on EventLog is a datetime . So it returns nil trying to match the exact datetime on a

has_many conditions or proc on foreign key

眉间皱痕 提交于 2020-01-14 18:08:43
问题 I have a has_many association between two models using a date as both the foreign and primary key for each model. It works perfectly one way but not the other. Works has_one :quiz_log, :primary_key => :start_at, :foreign_key => :start_at Doesn't work has_many :event_logs, :primary_key => :start_at, :foreign_key => :start_at The reason being (i think) because the start_at on QuizLog is a date and the start_at on EventLog is a datetime . So it returns nil trying to match the exact datetime on a

PostgreSQL, Rails and :order => problem

為{幸葍}努か 提交于 2020-01-10 19:38:22
问题 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

Rails: Using a Textarea for :has_many relationship

ぃ、小莉子 提交于 2020-01-06 12:53:11
问题 Hey guys, first question here. I have a couple of Products and Users that can put those Products on Wishlists. A User can have many Wishlists (for different purposes). Products can be added to Wishlists, but there's additional information involved: you have to specify an amount of a certain Product. This logic is used in the Inclusion, which has a field quantity . Class Wishlist belongs_to :user # User class is irrelevant here has_many :inclusions has_many :products, :through => :inclusions

Creating multiple participants in the User model

旧街凉风 提交于 2020-01-06 01:05:27
问题 I'm trying to set up a complex association where the User model is used two times. Once to create the leader of a discussion and then to create the participants for the discussion. First, do I have the associations correct in order to call multiple participants (users) for a discussion led by one user (leader)? User has_many :discussions, foreign_key: :leader_id belongs_to :received_discussions, class_name: 'Discussion' attr_accessible :participant_id Discussion belongs_to :leader, class_name

How to count MySQL results in a has-many-through relation

て烟熏妆下的殇ゞ 提交于 2020-01-05 03:47:06
问题 There is pretty good article how to filter results in a has-many relation: How to filter SQL results in a has-many-through relation I'm just seeking a solution for COUNT result, not show them all. student { id name } club { id name } student_club { student_id club_id } How many students are in both CLUB1 && CLUB2? EDIT: It would be great to use "Martin 2" method from a link below: SELECT s.stud_id, s.name FROM student s JOIN student_club sc USING (stud_id) WHERE sc.club_id IN (30, 50) GROUP

ruby on rails - how to make relationship works in route, controller, view ? has_many, belongs_to

纵然是瞬间 提交于 2020-01-02 04:38:06
问题 I am struggling to get my relationship in rails to work. I have a User,Gallery,Comment model class Gallery has_many :comments belongs_to :user end class User has_many :comments has_many :galleries end class Comment belongs_to :gallery belongs_to :user end now what should i do in the routes, controller and views to link this all up ? please help me ? its quite confusing finding out the answers. If can, I dont want it to be nested like in the railscast, but i want for each model, eg gallery i

Grails - Simple hasMany Problem - Using CheckBoxes rather than HTML Select in create.gsp

梦想的初衷 提交于 2020-01-02 02:47:09
问题 My problem is this: I want to create a grails domain instance, defining the 'Many' instances of another domain that it has. I have the actual source in a Google Code Project but the following should illustrate the problem. class Person { String name static hasMany[skills:Skill] static constraints = { id (visible:false) skills (nullable:false, blank:false) } } class Skill { String name String description static constraints = { id (visible:false) name (nullable:false, blank:false) description

Rails Question: belongs_to with STI — how do i do this correctly?

我怕爱的太早我们不能终老 提交于 2020-01-02 01:02:24
问题 I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many :babies end class Father < Parental has_many :babies end class Baby < ActiveRecord::Base belongs_to :?????? end What should Baby belong_to? In terms of a migration, what should i name/add for foreign key on the babies table? I've had a hard time researching

Rails Filter records of child model based upon the parent model attribute

谁都会走 提交于 2020-01-01 19:47:47
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads