has-many-through

rspec testing has_many :through and after_save

前提是你 提交于 2019-12-20 14:36:31
问题 I have an (I think) relatively straightforward has_many :through relationship with a join table: class User < ActiveRecord::Base has_many :user_following_thing_relationships has_many :things, :through => :user_following_thing_relationships end class Thing < ActiveRecord::Base has_many :user_following_thing_relationships has_many :followers, :through => :user_following_thing_relationships, :source => :user end class UserFollowingThingRelationship < ActiveRecord::Base belongs_to :thing belongs

HMT collection_singular_ids= deletion of join models is direct, no destroy callbacks are triggered

不想你离开。 提交于 2019-12-20 14:23:58
问题 Just ran into an issue with a has_many :through association and after/before-destroy callbacks not being triggered. Say, I have users, groups, and an intermediate relation called membership. I have a form that allows users to be enrolled into groups by creating a new membership record when they check off associated checkboxes. Basically an array of group_ids. Looks something like this: Which group would you like to join? (check all that apply) [] Group A [] Group B [] Group C And I wish to

has_many :through uninitialized constant

為{幸葍}努か 提交于 2019-12-20 12:36:20
问题 I've read the documentations and tons of tutorials about the has_many :through relations in Rails but I can't for the life of me get the hang of it. I'm trying to add a group to my current_user(devise) and I have a table in between Group and User with a status(The user's status is changeable for that group). Whenever I create a new Group now I get an error saying uninitialized constant Group::GroupUser here are my models: groupuser.rb class GroupUser < ActiveRecord::Base belongs_to :group

How to access fields in a customized many-to-many through object in templates

依然范特西╮ 提交于 2019-12-19 05:17:41
问题 Consider the following models: class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') class Membership(models.Model): person = models.ForeignKey(Person) group = models.ForeignKey(Group) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) Membership is a customized many-to-may through object with extra fields. If I have a

activerecord find through association

谁说我不能喝 提交于 2019-12-19 03:16:24
问题 I am trying to retrieve an activerecord object from my db. My models are class User < ActiveRecord::Base belongs_to :account has_many :domains, :through => :account end And class Account < ActiveRecord::Base has_many :domains has_many :users end And class Domain < ActiveRecord::Base belongs_to :account end Now I would like to retrieve a user based on the username and a domain name (lets assume that these are attributes of the User and the Domain classes respectively). i.e. something along the

Rails has_many :through PG::Error: ERROR: column reference “id” is ambiguous error

冷暖自知 提交于 2019-12-19 02:06:09
问题 I'm new to rails and I've been trying to get two has_many :though relationships to work out (as opposed to using has_and_belongs_to_many as explained by this post http://blog.flatironschool.com/post/35346328762/why-you-dont-need-has-and-belongs-to-many) but am now running into a Postgres error: PG::Error: ERROR: column reference "id" is ambiguous LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM... ^ : SELECT 1 AS one FROM "components" INNER JOIN "collection_components"

many-to-many: has_many :through association form with data assigned to linking model create form view

旧街凉风 提交于 2019-12-18 12:44:34
问题 I'm playing with an example from Rails Guides: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association This example has the following setup for the 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 I'm

has_many :through with a foreign key?

元气小坏坏 提交于 2019-12-18 10:35:28
问题 I've read multiple questions about this, but have yet to find an answer that works for my situation. I have 3 models: Apps , AppsGenres and Genres Here are the pertinent fields from each of those: Apps application_id AppsGenres genre_id application_id Genres genre_id The key here is that I'm not using the id field from those models. I need to associate the tables based on those application_id and genre_id fields. Here's what I've currently got, but it's not getting me the query I need: class

Rails: has_many through with polymorphic association - will this work?

夙愿已清 提交于 2019-12-18 10:06:30
问题 A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record? Here are the models I have: class Event < ActiveRecord::Base belongs_to :person belongs_to :eventable, :polymorphic => true end class Meal < ActiveRecord::Base has_one :event, :as => eventable end class Workout < ActiveRecord::Base has_one :event, :as => eventable end The main question concerns the Person class: class Person <

Howto use callbacks in a has_many through association?

喜欢而已 提交于 2019-12-18 03:03:43
问题 I have a Task model associated to a Project model via has_many through and need to manipulate the data before delete/insert via the association. Since "Automatic deletion of join models is direct, no destroy callbacks are triggered." i can not use callbacks for this. In Task i need all project_ids to calculate a value for Project after Task is saved. How can i disable delete or change delete to destroy on has_many through association? What is best practise for this problem? class Task has