has-many-through

Issues with has_many :through, cache, touch and counter_cache

I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers. The problem is, that join table entries are deleted, not destroyed. If relation is gone, I have ...

Get join model from has_many_through relation

Hi, In my Rails app I have a has_many_through relation. I want to use the join model/table to store some data about the relation (how many times that particular relation is used, to be specific). I'm writing an add-method on one of my classes which should check for any existing relation with the subject, update a counter on the relatio...

In rails, how to destroy an 'join table item' with out delete the real record?

Hi guys: I get confuse now, I don't know how to delete/destroy a record in a join table: class Task < ActiveRecord::Base belongs_to :schema belongs_to :to_do end class Todo < ActiveRecord::Base belongs_to :schema has_many :tasks end class Shcema < AcitveRecord::Base has_many :todos has_many :tasks, :through => :todos e...

Rails3 nested has_many through question

We are planning to upgrade our application to Rails3. One plugin we've used quite a bit is nested_has_many_through. This plugin seems outdated, and no longer maintained, and simply does not appear to be working in a new Rails3 application. A simple example: Author.rb has_many :posts has_many :categories, :through => :posts, :uniq => tr...

RESTful routes for has_many :through relationships?

I have 2 models - User and Activity - which are related by a has_many :through using the UserActivity model. A User can either "want" or "done" an activity, which creates a UserActivity record and sets the appropriate boolean. What would you recommend for handling these actions when creating routes and controller actions? Would somethin...

Linking two tables together using has_many AND has_one?

I sure this has been asked a million times already. I just not searching very well. I have the following setup: I have an Instructor who has many Events. Each event has only one Instructor (owner/creator). I want to add a separate linkage to allow other instructors to be associated but still preserve the original instructor (owner). I ha...

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

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

Rails: setting up has_many_through association between tables from different databases

I am trying to setup a has_many :through relationship between two models User and CustomerAccount through another join model AccountOwnership (users and account_ownerships tables are in one db, say db1 and the customer_accounts table is in remote db, say db2). Here is the relevant code, that sets up the associations class User < Activ...

Django - What's a good way to handle manytomany with intermediary table in forms and views

Suppose I have the following models - class Item(models.Model): name = models.CharField(max_length=150) value = models.DecimalField(max_digits=12,decimal_places=2) class Organization(models.Model): name = models.CharField(max_length=150) items = models.ManyToManyField(Item, through='Customizable') class Customizable(m...

has many polymorphic images through another polymorphic model

Greets to ruby developers. I'm stuck with ActiveRecord model associations. I illustrate what I want to do by a tables. I have these tables: Products: +----+-----------+ | id | name | +----+-----------+ | 1 | Cellphone | +----+-----------+ Images: +----+-------+--------------+----------------+ | id | url | imageable_id | image...

Sorting rows by count of a many-to-many associated record

I know there are a lot of other SO entries that seem like this one, but I haven't found one that actually answers my question so hopefully one of you can either answer it or point me to another SO question that is related. Basically, I have the following query that returns Venues that have any CheckIns that contain the searched Keyword ...

Setting a :has_many :through association on a belongs_to association Ruby on Rails

I have three models each having the following associations class Model1 < ActiveRecord::Base has_many :model2s has_many :model3s end class Model2 < ActiveRecord::Base belongs_to :model1 has_many :model3s, :through => :model1 #will this work... is there any way around this end class Model3 < ActiveRecord::Base belongs_to :model1 has_m...

has_many through issues when adding attributes to the join table

I have models moves, neighborhoods and communities. Neighborhood.rb has_many :communities has_many :moves, :through => :communities accepts_nested_attributes_for :communities Move.rb has_many :communities has_many :neighborhoods, :through => :communities accepts_nested_attributes_for :communities Community.rb belongs...

Rails has_many through fails on create method

Hi, Say I have 3 Models: User has_many :user_projects has_many :projects, :through => :user_projects Project has_many :user_projects, :dependent => :destroy has_many :users, :through => :user_projects, :uniq => true UserProject belongs_to :project belongs_to :user I then have a form that allows the creation of a new Project and c...

Seemingly redundant ActiveRecord has_many definition

I've been applying has and belongs to many style associations to a project I've been working on, but because I wanted to capture extra information in my join model, I'm implicitly setting it up via the more elaborate belongs_to/has_many approach instead of has_and_belongs_to_many. After initially stumbling on this, I learned that I neede...

rails 3 has_many :through record save error

I'm not exactly sure what my problem is, so this question may require some more clarification, but here's what seems to be most relevant: I have a has_many :through and the join model has some fields that aren't foreign keys. When I build the models up and try to save I get a validation error on the non-foreign key fields from the join...

Rails Select options not working

After going through the process of updating rails to 2.3.8 my select option stopped working in my view. For some reason it now tells me, even when it has something selected that it can't be empty. I have found this to be quite annoying and decided after hours of fighting with it to enlist some help. Here's what I have so far. new.html.e...

Using "has_many through" I can't update or add new records, also using rails3 with formtastic for forms.

Why can't I add new items to my database using a has_many :through relationship? I am missing something, the form shows up right, but I just can't add anything to my database. I hit enter and nothing happens. I'm running mysql / rails3 / using formtastic. Subscriber.rb (Subscriber model) class Subscriber < ActiveRecord::Base attr_acc...

Rails RSpec Tests for a has_many :through Relationship

I'm new to testing and rails but i'm trying to get my TDD process down properly. I was wondering if you use any sort of paradigm for testing has_many :through relationships? (or just has_many in general i suppose). For example, i find that in my model specs i'm definitely writing simple tests to check both ends of a relationship for r...

Right way to force uniqueness on a join model? (has_many :through)

I have a parent/child relationship via our users table, with models as such: class User < ActiveRecord::Base # Parents relationship has_many :children_parents, :class_name => "ParentsChild", :foreign_key => "child_id", :dependent => :destroy has_many :parents, :through => :children_parents # Children relatiopnship has_many :...