has-many-through

Rails: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s)

一笑奈何 提交于 2019-11-30 11:50:24
I have the following code (somewhat simplified ... create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestamps end With the models looking like ... class Signature < ActiveRecord::Base belongs_to :card belongs_to :user end class Card < ActiveRecord::Base has_many :signatures has_many :signers, :through => :signatures, :foreign_key => "card_id" end class User < ActiveRecord::Base has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id" has_many :received_cards, :class_name => "Card", :foreign_key => "recipient_id" has_many :signatures has_many :signed

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

Rails: HasManyThroughAssociationNotFoundError

社会主义新天地 提交于 2019-11-30 10:33:41
问题 I have problems with getting a has_many through association to work. I keep getting this exception: Article.find(1).warehouses.build ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article These are the models involved: class Article < ActiveRecord::Base has_many :warehouses, :through => :entries end class Warehouse < ActiveRecord::Base has_many :articles, :through => :entries end class Entry < ActiveRecord::Base belongs_to :article

has_many :through questions

我的未来我决定 提交于 2019-11-30 09:48:18
I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....: class Game < ActiveRecord::Base has_many :game_users, :dependent => :destroy has_many :users, :through => :game_users, :uniq => true end class User < ActiveRecord::Base has_many :game_users, :dependent => :destroy has_many :games, :through => :game_users, :uniq => true end class GameUser < ActiveRecord::Base belongs_to :game belongs_to :user end And my database migration for the join

How to populate fields in a has_many through join table

自古美人都是妖i 提交于 2019-11-30 07:17:39
I have a question concerning active record association, referring to this part of the rails documentation: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association if we have three models: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base has_many :appointments has_many :physicians, :through => :appointments end The documentation says that the collection of join models can be managed via

Activeadmin formtastic dynamic select

Deadly 提交于 2019-11-30 07:15:58
问题 I would like to make a dynamic select option via Activeadmin 's formtastic like so: form do |f| f.inputs "Exam Registration Details" do f.input :user_id, :as => :select, :collection => User.where(:admin => 'false') #selects user from list. WORKING f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user) #collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons. f.input :lesson_id, :as =>

How can I create new records with has_many :through and honor :conditions?

試著忘記壹切 提交于 2019-11-30 07:12:41
Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :memberships has_many :students, :through => :memberships has_many :observers, :through => :memberships, :source => :student, :conditions => { :memberships => { :observer => true }} end Here's what works great: observers = Course.find(37).observers Here's what doesn't work: new_observer = Course.find(37).observers

Validate that an object has one or more associated objects

我只是一个虾纸丫 提交于 2019-11-30 02:47:45
I need to ensure that when a product is created it has atleast one category. I could do this with a custom validation class, but I was hoping there was a more standard way of doing it. class Product < ActiveRecord::Base has_many :product_categories has_many :categories, :through => :product_categories #must have at least 1 end class Category < ActiveRecord::Base has_many :product_categories has_many :products, :through => :product_categories end class ProductCategory < ActiveRecord::Base belongs_to :product belongs_to :category end wpgreenway There is a validation that will check the length of

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

Rails: HasManyThroughAssociationNotFoundError

荒凉一梦 提交于 2019-11-29 20:52:47
I have problems with getting a has_many through association to work. I keep getting this exception: Article.find(1).warehouses.build ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article These are the models involved: class Article < ActiveRecord::Base has_many :warehouses, :through => :entries end class Warehouse < ActiveRecord::Base has_many :articles, :through => :entries end class Entry < ActiveRecord::Base belongs_to :article belongs_to :warehouse end And this is my schema: create_table "articles", :force => true do |t| t.string