has-many-through

RoR: Destroy association with has_many, :through if orphaned

Given Ryan Bates's great tutorial on Virtual Attributes, how would I go about destroying a Tag (not Tagging) if, once the article is destroyed, that Tag is no longer used? I tried doing something like this: class Article < ActiveRecord::Base ... after_destroy :remove_orphaned_tags private def remove_orphaned_tags tag...

ROR Self referential has_many through with accept_nested_attributes_for

Hi. I wondered if someone could take a quick look at this. I'm making a simple conversion application which converts between units of measurement. I need to be able to self reference the table using a join table which stores the relationship between each, along with the conversion between each. This then would be referenced between eithe...

Rails has_many through singular association

This is probably quite simple but I haven't yet been able to wrap my head around the problem. I have 3 tables... (well more than that) but in this scenario 3 that matter. Places Bookings and Ratings Places has_many bookings Each booking has_one rating (because the user only rates once) and belongs_to (a) Place Ratings belong_to (a) ...

Problem with has_many :through and fields_for.

Hi There, I have a pretty basic association: # user.rb class User < ActiveRecord::Base has_many :services, :through => :subscriptions has_many :subscriptions, :accessible => true accepts_nested_attributes_for :subscriptions end # service.rb class Service < ActiveRecord::Base has_many :users, :through => :subscriptions has_ma...

Using has many :through

Using has_many => through association. Here is what i have. :planning model has_many :acttypes has_many :actcategories has_many :acts, :through => :actcategories :acts model belongs_to :acttype has_many :actcategories has_many :plannings, :through => :actcategories :actcategories model named_scope :theacts, lambda { |my_id| ...

Rails association errors possibly related to typus

I have a problem with associations. As a newbie to RoR, I've learned about associations from the guide on the RoR site. I have followed one of the example almost to the letter, the only thing being changed are the class names. The example being the following: class Document < ActiveRecord::Base has_many :sections has_many :pa...

Invalid source reflection macro :has_many :through

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_subprogra...

Ruby on Rails - has_many :through with nested_attributes_for

I'm coming across a problem that I can't find much online for via Google, forums, groups, etc. and so I'm going to raise my hand and ask for help from those who are more wise than I :) I have a rails project setup that was using nested_attributes_for with a one-to-one relationship between two models. It worked quite easily and as expec...

Updating an associated record from a join table model

I have a has_many :through relationship associating players to teams THROUGH managements. I want to have a counter on the teams table (a bit like a counter cache) that tells me how many new associations there have been since the beginning of the week. Of course a counter cache wont work because it will always give all the associations t...

Specifying the foreign key in a has_many :through relationship

I have the following three models: User, Project, and Assignment. A User has_many Projects through an assignment. However, Assignment actually has two foreign keys that relate to a User: user_id (representing the user who was assigned the project) and completer_id (representing the user who completed the project). Often, user_id and co...

Rails: has_many with extra details?

While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in th...

Rails class name/type not working for a polymorphic has_many :through

I have an invoicing system that manages debits and credits. Basically the invoice amount is obtained by the sum of its debits and the balance is derived by taking the sum of its credits and subtracting it against the total amount. I'm doing this with four models. Invoice Line Item Debit Credit The way it works is via a join model (L...

How do I make an exclusive rather than inclusive query with named scopes in Rails?

I've created an application that finds service providers based on multiple services passed to it. This is done via has many_through so there is a join object. Basically, if a user asks for service providers with Service A and Service B my scope returns service providers who offer either BOTH Service A and Service B or other service provi...

rails has_many relationship (4 models) and how to access in the view

I have 4 models: transac, transac_data, item, dvd_details class Transac < ActiveRecord::Base has_many :transac_datas has_many :items, :through => :transaction_datas end class TransactionData < ActiveRecord::Base belongs_to :item belongs_to :transaction end class Item < ActiveRecord::Base has_many :transaction_datas has...

Filtering join table in has_many :through in RoR

I have the following models in which I join the Language and Products table via the Translation table using the Rails has_many :through paradigm: class Language < ActiveRecord::Base has_many :translations has_many :products, :through => :translations end class Translation < ActiveRecord::Base belongs_to :product belongs_to :lan...

Nested Has Many Through Plugin and Named Scopes

I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model (:name) [>all singular<] Then I created two join tables -> UsersInterests and EventsInterests; each not containing a primary key and only comprised of the user_id/interest_id and event_id/interest_id respectively. [>plural<] My Models Use the...

Use ActiveRecord to Find Result of 5 Nested Tables

I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model (:name) Then I created two join tables -> UsersInterests and EventsInterests; each not containing a primary key and only comprised of the user_id/interest_id and event_id/interest_id respectively. I'm trying to use ActiveRecord to query a lis...

Using fields from an association (has_many) model with formtastic in rails

I searched and tried a lot, but I can't accomplish it as I want.. so here's my problem. class Moving < ActiveRecord::Base has_many :movingresources, :dependent => :destroy has_many :resources, :through => :movingresources end class Movingresource < ActiveRecord::Base belongs_to :moving belongs_to :resource end class Resource <...

How can one obtain a row count from has_many :through relations with :uniq => true

This is my model: class Tag < ActiveRecord::Base # id, name has_many :taggings end class Tagging < ActiveRecord::Base # id, tag_id, owner_id, target_type, target_id belongs_to :tag belongs_to :owner, :class_name => 'User' belongs_to :target, :polymorphic => true validates_uniqueness_of :tag_id, :scope => [ :target_id, :ta...

How do I order a has_many through association in Ruby on Rails?

Given the following AR models, I would like to sort users alphabetically by last name when given a handle to a task: #user has_many :assignments has_many :tasks, :through => :assignments #assignment belongs_to :task belongs_to :user #task has_many :assignments has_many :users, :through => :assignments I would like to get a task ...