has-many

how to avoid duplicates in a has_many :through relationship?

风流意气都作罢 提交于 2019-11-30 10:57:16
问题 How can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them: class Blog < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :readers, :through => :blogs_readers end class Reader < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :blogs, :through => :blogs_readers end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end What I

Multiple hasMany relationships to same domain class in Grails

做~自己de王妃 提交于 2019-11-30 07:26:18
I'm using Grails, and I have a domain model with multiple hasMany attributes to the same domain class, which looks like this: static hasMany = [ posts : Post, likes : Post, dislikes : Post ] The problem that I'm running into is that when I add something to the posts list, it also somehow makes it into the likes and dislikes lists. At least, that's how it looks when I iterate through each of those lists. I think that the issue is that I also have the following relationship in my Post domain: static belongsTo = [ contributer : Contributer ] What is the best way of going about configuring these

Examples of new, create actions for has_many :through associations (nested)

我怕爱的太早我们不能终老 提交于 2019-11-30 06:48:32
问题 I found this code on http://guides.rubyonrails.org/association_basics.html#the-has_one-through-association: class Document < ActiveRecord::Base has_many :sections has_many :paragraphs, :through => :sections end class Section < ActiveRecord::Base belongs_to :document has_many :paragraphs end class Paragraph < ActiveRecord::Base belongs_to :section end This is exactly what I'm trying to accomplish, but I'm still very new to Rails and was wondering if someone could show me a sample of the forms

rails model has_many of itself

喜夏-厌秋 提交于 2019-11-30 06:45:24
问题 I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event.child_event or event.parent_event . But my googling hasn't worked out that well. My Model: class Event < ActiveRecord::Base attr_accessible :days_before, :event_name, :event_date, :list_id, :recipient_email, :recipient_name, :parent_event_id, :is_deleted, :user_id belongs_to :user has_many :event_email

Rails: Non id foreign key lookup ActiveRecord

旧街凉风 提交于 2019-11-30 03:02:51
I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David') ('David' being the that coach's user_name ) user_name is

ActiveAdmin with has_many problem; undefined method 'new_record?'

喜夏-厌秋 提交于 2019-11-29 23:25:58
I'm trying to customise a ActiveAdmin form for a Recipe model that has a has_many relationship with Step. class Recipe < ActiveRecord::Base has_many :steps end class Step < ActiveRecord::Base acts_as_list :scope => :recipe belongs_to :recipe end I have the following in my ActiveAdmin file with relation to this: form do |f| f.has_many :steps do |ing_f| ing_f.inputs end end The following error is thrown when I try to load the form: undefined method `new_record?' for nil:NilClass I've isolated it so far to the has_many method but I'm lost past this. Any advice and help would be appreciated! Dan

how to avoid duplicates in a has_many :through relationship?

拥有回忆 提交于 2019-11-29 22:57:00
How can I achieve the following? I have two models (blogs and readers) and a JOIN table that will allow me to have an N:M relationship between them: class Blog < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :readers, :through => :blogs_readers end class Reader < ActiveRecord::Base has_many :blogs_readers, :dependent => :destroy has_many :blogs, :through => :blogs_readers end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end What I want to do now, is add readers to different blogs. The condition, though, is that I can only add a

Nested attributes for belongs_to association rails

蓝咒 提交于 2019-11-29 17:44: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

Multiple hasMany relationships to same domain class in Grails

北慕城南 提交于 2019-11-29 09:35:54
问题 I'm using Grails, and I have a domain model with multiple hasMany attributes to the same domain class, which looks like this: static hasMany = [ posts : Post, likes : Post, dislikes : Post ] The problem that I'm running into is that when I add something to the posts list, it also somehow makes it into the likes and dislikes lists. At least, that's how it looks when I iterate through each of those lists. I think that the issue is that I also have the following relationship in my Post domain:

Sequelize onDelete not working

走远了吗. 提交于 2019-11-29 08:17:05
I have associations between two models defined as below: For Contact Model (in a separate file) classMethods: { associate: function (models){ Contact.belongsTo(models.User) } } For User(in a separate file) classMethods: { associate: function (models){ User.hasMany(models.Contact, {onDelete: 'CASCADE'}) } } The problem is when deleting the user the contact related to the user is not deleting any advice on what am I doing wrong would be helpful? Thanks in advance I think you need to define it the other way around. Contact.belongsTo(models.Users, { foreignKeyConstraint: true , onDelete: 'cascade'