has-many-through

has_many :through + polymorphic relationships

血红的双手。 提交于 2019-12-03 00:43:11
I using rails3 and trying to build some complex associations. I have Product, Version and Property models. class Version < ActiveRecord::Base belongs_to :product has_many :specs has_many :properties, :through => :specs end class Product < ActiveRecord::Base has_many :versions has_many :specs has_many :properties, :through => :specs end class Property < ActiveRecord::Base end class Spec < ActiveRecord::Base belongs_to :product belongs_to :spec belongs_to :version end It works perfect, but i want to use product and version as polymorphic relations, so table specs will have only spec_id and some

Filtering child objects in a has_many :through relationship in Rails 3

女生的网名这么多〃 提交于 2019-12-02 20:44:01
Greetings, I have an application where Companies and Users need to belong to each other through a CompanyMembership model, which contains extra information about the membership (specifically, whether or not the User is an admin of the company, via a boolean value admin ). A simple version of the code: class CompanyMembership < ActiveRecord::Base belongs_to :company belongs_to :user end class Company < ActiveRecord::Base has_many :company_memberships has_many :users, :through => :company_memberships end class User < ActiveRecord::Base has_many :company_memberships has_many :companies, :through

Rails has_many :through with collection_select form helper

本小妞迷上赌 提交于 2019-12-02 18:17:09
问题 I'm having issues with creating a form that will save my has_many :through associations. I've successfully saved by posting json, but the forms just won't work for me yet. The request params created by the form submit just won't work out. Any help pointing me to the solution would help me from losing any more time on this. Thanks up front. EDITED -- Added forms_for attempt and the created params json that doesn't work as well at the bottom -- Json post request params that works: { "author": {

Avoid Django def post duplicating on save

痞子三分冷 提交于 2019-12-02 15:03:23
问题 Hi I'm facing issues of duplicated objects when saving. How can I prevent that? Thanks in advance. #models.py class Candidate(models.Model): user = models.OneToOneField(User, primary_key=True) birth = models.CharField(max_length=50) ... class Job(models.Model): candidate = models.ManyToManyField('Candidate', through='CandidateToJob') title = models.CharField(max_length=500) ... class CandidateToJob(models.Model): job = models.ForeignKey(Job, related_name='applied_to') candidate = models

Rails has_many :through with collection_select form helper

冷暖自知 提交于 2019-12-02 08:00:34
I'm having issues with creating a form that will save my has_many :through associations. I've successfully saved by posting json, but the forms just won't work for me yet. The request params created by the form submit just won't work out. Any help pointing me to the solution would help me from losing any more time on this. Thanks up front. EDITED -- Added forms_for attempt and the created params json that doesn't work as well at the bottom -- Json post request params that works: { "author": { "name": "Author Name", "post_authors_attributes": [ {"post_id":"1"}, {"post_id":"2"}, {"post_id":"3"}

Invalid source reflection macro :has_many :through

天涯浪子 提交于 2019-12-02 00:44:10
I have such angry associations: financings >- events >- subprograms >- programs. I want to get acces to last_financings from programs through all of them so code is: class Fcp < Program has_many :fcp_subprograms, :foreign_key => 'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :source => :last_actual_financings class FcpSubprogram < Program belongs_to :fcp, :class_name => 'Fcp', :foreign_key => 'parent_id' has_many :events, :foreign_key => 'fcp_id' has_many :last_actual_financings, :through => :events, :source => :last_actual_financings class Event <

Rails 4: counter_cache in has_many :through association with dependent: :destroy

梦想的初衷 提交于 2019-12-01 21:08:05
Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models, with a has_many :through association : class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through: :administrations end class Administration < ActiveRecord::Base belongs_to :user belongs_to :calendar end The join

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

徘徊边缘 提交于 2019-12-01 03:31:29
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 person instance, how can I access the corresponding date_joined fields of all its membership relations -

activerecord find through association

家住魔仙堡 提交于 2019-11-30 21:12:17
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 lines of User.find(:first, :conditions =>{:username => "Paul", :domains => { :name => "pauls-domain"}}

How to save many has_many_through objects at the same time in Rails?

假如想象 提交于 2019-11-30 18:57:45
问题 I have two models related as follows. USERS has_many :celebrations has_many :boards, :through => :celebrations BOARDS has_many :celebrations has_many :users, :through => :celebrations CELEBRATIONS :belongs_to :user :belongs_to :board In my controller I want to create the objects from form data. I do this as follows: @user = User.new(params[:user]) @board = Board.new(params[:board]) if @user.save & @board.save @user.celebrations.create(:board_id => @board,:role => "MANAGER") redirect_to