Rails 3 - Has_And_Belongs_To_Many

Rails 3, HABTM, and Displaying data

you can just access it directly:

<td> <%= user.roles.collect(&:name).join(",") %> </td>

Rails 3 HABTM yields ActiveRecord::Associations::CollectionProxy objects

Having a hard time trying to find the error here.. But maybe some of these will help you:

I think you are missing the multiple field index:

add_index : volunteer_circles_users, [:volunteer_circles_id, :user_id]
add_index : volunteer_circles_users, [:user_id, :volunteer_circles_id]

And the create_join_table creates it in alphabetical order, so it would be user_volunteer_circles instead. So, maybe you could need to do something like:

class VolunteerCircle < ActiveRecord::Base 
has_and_belongs_to_many :users, :join_table => : volunteer_circles_users
end

Hope it helps..

Getting previous HABTM values

I had a similar problem in an app, and the only solution I could come up with was to store the values before doing update_attributes in the controller.

Example code:

Models

class Product < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => "categories_products"

def remember_prev_values(values)
@prev_values = values
end

def before_update_do_something
puts @prev_values - self.category_ids # Any categories removed?
puts self.category_ids - @prev_values # Any categories added?
end
end

class Category < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => "categories_products"
end

In the update method in the products controller I do the following:

class ProductsController < ApplicationController
...

def update
@product.remember_prev_values(@product.category_ids)
if @product.update_attributes(params[:product])
flash[:notice] = "Product was successfully updated."
redirect_to(product_path(@product))
else
render :action => "edit"
end
end

...

end

It is not ideal, but it is then possible to "catch" the habtm inserts/removes before they are executed.

I do think it is possible to do in a callback, but you might need to "hack" into ActiveRecord.

I did not spend much time on trying to dig into ActiveRecord internals, as this is a simple implementation that works.

Rails 3 HABTM one-liner

Ohh wait haha, I think this might do the trick:

rails g model articles_tags article:references tag:references --no-id --no-timestamps

I wonder if there is anyway to suppress the model file's creation (article_tags.rb) so that I can just use the standard has_and_belongs_to_many syntax without having to specify a :through param? I'm looking for the ultimate one-liner: Tip of the hat to anyone who can improve the above one-liner to enable only the use of has_and_belongs_to_many syntax without a join model! Else, I'm going back to Django, with it's built-in ManyToManyFields.

Rails 3 collection_select helper method for a HABTM relationship

I you haven't figured this out, it will work if you pass in :presenter_ids as the second parameter rather than :presenters. In the end, you are just mapping the selected ids to the model's id collection. The error is saying "You tried to assign a string to a collection of Presenters".

Ruby on Rails 3: HABTM with new controller action

The second line of your controller action should be:

@user.projects += [Project.find(params[:id])]

or even

@user.project_ids += [params[:id]]

This will append the passed project to the existing project collection. The User#projects= method (defined by the habtm association) expects a collection not a Project object. Internally, it tries to call each so it fails since a Project doesn't respond to each.

Rails 3 - join table not updated when creating new HABTM association

  1. Use plurals for has_many associations
  2. Build the album directly within the user's albums

    @album = @user.album.create(params[:album])

How do I reference data in an HABTM table without a model in Rails 3?

I'm not sure I fully understand your question, but Project.where(:user_id => current_user) should become current_user.projects

And to add user with id 1 to project with id 3 you'd do

Project.find(3).users << User.find(1)

Is this what you've been trying to do ?



Related Topics



Leave a reply



Submit