has-and-belongs-to-many

Rails: How to observe join records that don't actually have a Model?

Dear Stack, Is it possible, using an Observer, to observe the creation of JOIN records? For example, you have a User Model that has_and_belongs_to_many Book Models. Is it possible to monitor books_users records as they are created or deleted or must I have a BookUser model to do this? Example of what I want to observe: User.books <<...

Trying to remove a has_and_belongs_to_many relationship in rails

So I just started my first rails project yesterday. I had two many-to-many (has_and_belongs_to_many) relationships in my application. I had one between models games and teams and another between models stats and results. This was all working just fine by creating the join table myself with a migration. I then decided that I did not w...

Save has_and_belongs_to_many child

I have a User model and a Role model. They are joined by a has_and_belongs_to_many relationship. When an admin creates a user I want them to be able to assign a role to the user and have it saved when I call @user.save The thing is though is that I get a warning that I can't mass-assign the roles relationship. Any suggestions on how to...

How can ActiveResource create HABTM relationships?

I am trying to add multiple HABTM relationships through ActiveResource, but I am running into a lot of trouble. It seems that in a traditional rails app (i.e. all ActiveRecord), you can simply set your HABTM ids, and do something as such: habtm = {:category_ids => [1,2,3]} article.update_attributes(habtm) Unfortunately, when I try to...

Rails has_and_belongs_to_many is confusing me with fixtures and factories

General Confusion I have bands which can have 3 genres. I read in a previous SO post that the proper way to handle this is a couple steps: 1) In band.rb has_and_belongs_to_many :genres 2) Create a band_genres join table Even after reading the documentation, I am a bit confused as to what HABTM actually means. I guess I would just n...

Double join with habtm in ActiveRecord

I have a weird situation involving the need of a double inner join. I have tried the query I need, I just don't know how to make rails do it. The Data Account (has_many :sites) Site (habtm :users, belongs_to :account) User (habtm :sites) Ignore that they are habtm or whatever, I can make them habtm or has_many :through. I want to b...

Ruby on Rails prevent nil error when it is assumed record may not exist

I am building a simple book check out application. One of the things that I need to do is determine if a book is checked out. I have my associations between my people and book classes setup through a book_check_out class. My goal is to use the checked_out property of book to determine if a book is presently checked out. However, in my pr...

How to create has_and_belongs_to_many associations in Factory girl

Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional association? Here's my attempt Factory.define :company do |f| f.users{ |users| [users.a...

How to manage n:n relationships with Rails?

Let's say I have a Company that has many Employees and each Employee can have many Companies. Basically I will have : class Employee < ActiveRecord::Base has_and_belongs_to_many :companies end and class Company < ActiveRecord::Base has_and_belongs_to_many :employees end But then I'm confused about how I could get things like: ...

Ordering of has_and_belongs_to_many associations

In my rails app I have two models that are related by has_and_belongs_to_many. This means there is a join table. Imagine the scenario where I am adding users to a game. If I want to add a user, I do: @game.users << @user Supposing that I want to know in what order I added these users. I can do this: @game.users.each do.... My qu...

Trying to use accepts_nested_attributes_for and has_and_belongs_to_many but the join table is not being populated

I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information that would traditionally be two forms. I have read on some sites they are compatible, some sites they aren't compatible, and some sites don't know. As reference, I am using Rails 2.3.4. I tried modeling my solution from ...

validate presence of has_and_belongs_to_many

Hi i'm using a has_and_belongs_to_many in a model. I want set the valitor of presence for kinds. and set the max number of kinds per core to 3 class Core < ActiveRecord::Base has_and_belongs_to_many :kinds, :foreign_key => 'core_id', :association_foreign_key => 'kind_id' end how can i do? thanks ...

Complex forms - Managing multiple models in a single form - (3rd degree madness)

I have isolated the problem in the second block of code below (if you don't want all the details): I can create new users from the account model. I can't assign those users roles from the accounts model. I am using fields_for, this method does not work when I attempt to assign the role_ids to the roles model. My db is set up in the follo...

Is it possible to interact with a join table, with a form's create action?

*Note to view all code, follow the link I am creating new "accounts". Each new Account have_many :users. Users have_and_belong_to_many roles. I am trying to assign EXISTING roles to new users, in the new account form. Typically an easy process however... The have_and_belong_to_many association between Users and Roles implies that user...

update values of checkbox with HABTM relationship -- Rails

Hey guys I've been using the has_and_belongs_to_many relationship with checkboxes example from the Railscast Episode #17 . I had some problems and now everything is working kind of smoothly except the update button will not work. the edit view looks like so <% form_for :users, :action => 'update' do |f| %> <% for interest in Intere...

acts_as_list with has_and_belongs_to_many relationship

I've found an old plugin called acts_as_habtm_list - but it's for Rails 1.0.0. Is this functionality built in acts_as_list now? I can't seem to find any information on it. Basically, I have an artists_events table - no model. The relationship is handled through those two models specifying :has_and_belongs_to_many How can I specify ord...

rails HABTM joins and inconveniences

I have a working HABTM association between the models Posts and Users... the posts_users table is as advertised and both have the necessary has_and_belongs_to_many in their model.rb And if I use User.find(1).posts it will find all the posts with the valid username My question is how to deal with this situation. I want to use user.po...

HABTM checking for match of latest 3

Here's an interesting one for you folks... I have a HABTM (has_and_belongs_to_many) relationship between "Dogs" and "Trips". My goal is to find two result sets: 1) Dogs that have been on at least 1 of the last 3 trips and call that @dogs_current 2) Dogs that have NOT been on any of the last 3 trips and call that @dogs_old I found that...

Simulating a belongs_to_many in rails

I have a situation where in theory I would need to use a belongs_to_many relationship. I have an Example model and a Sentence model. Each example object has one sentence but these sentences are not necessarily unique. So, for example, I could have two example models that each have one sentence that is the same sentence. I'm not sure ...

Associating Models with Polymorphic

I am trying to associate Contacts with Classes but as two different types. Current_classes and Interested_classes. I know I need to enable polymorphic but I am not sure as to where it needs to be enabled. This is what I have at the moment class CreateClasses < ActiveRecord::Migration def self.up create_table :classes do |t| ...