Rails 3.1 - has_and_belongs_to_many deprecated?
It's deprecated, but still there. It doesn't check to see if you have attributes on the join table, it's just letting you know. It still works.
Should I use a has_and_belongs_to_many relation?
in this case,The has_many :through Association will work great
sponsor.rb
has_many :sponsor_photos, :dependent => :destroy
has_many :photos, :through => :sponsor_photos
photo.rb
has_many :sponsor_photos, :dependent => :destroy
has_many :sponsors, :through => :sponsor_photos
sponsor_photo.rb
##holding only sponsor_id and photo_id
belongs_to :sponsor
belongs_to :photo
###########so now you can use################
@sponsor=Sponsor.first
##get all photos of a sponsor
@sponsor.photos
@photo=Photo.first
##get all sponsors from a photo
@photo.sponsors
HOPE THIS HELPS...
Rails 3 has_many :through naming issue
orders_products
is not the correct naming convention for a has_many :through
relationship--it is correct for a has_and_belongs_to_many
relationship. In a has_many :through
, the "join" model is not just for joining--it is also its own model that has its own data, that also happens to join two other models together.
If your OrderProduct
model doesn't have any of it's own data, logic, or constraints, then you could use a has_and_belongs_to_many
relationship instead, remove the model completely, and then your join table is named right. Otherwise, it is named according to regular model naming conventions, namely order_products
.
Using uniq in a has_and_belongs_to_many relationship in Rails 4
class User
has_and_belongs_to_many :foos, -> { uniq }
end
According to the documentation here
why we need to explicitly define join table in HABTM?
Rails is smart here too. You don't always have to tell the name of the join table to Rails; Rails is intelligent enough to figure it out. You just have to create a migration to generate the table, and all the other tasks will be taken care by Rails.
Let's say, you have following two tables:
Author
Book
An author authors many books, and a book is authored by many authors. Rails will automagically prediect the join table name: authors_books
- you just have to write migration to create it.
And then, the task is dead simple.
author = Author.first
author.books # all the books authored by the first author
book = Book.first
book.authors # all the authors of the first book
new_book = Book.new
author.books << new_book # add a new book to an author
author.save
new_author = Author.new
books.author << new_author # add a new author to a book
Remember, it doesn't have to be always this way. You can change the table name as well.
rails model has_many :through associations
Ok, so your WorkoutExercises table can't be empty. This is how it should look:
class CreateWorkoutExercises < ActiveRecord::Migration
def change
create_table :WorkoutExercises do |t|
t.integer :exercise_id, :null => false
t.integer :workout_id, :null => false
t.timestamps
end
# I only added theses indexes so theoretically your database queries are faster.
# If you don't plan on having many records, you can leave these 2 lines out.
add_index :WorkoutExercises, :exercise_id
add_index :WorkoutExercises, :workout_id
end
end
Also, you can name this table whatever you'd like, it doesn't have to be WorkoutExercises.
However, if you were using a has_and_belongs_to_many relationship, your table would have to mandatorily be named ExercisesWorkout. Notice how Exercises comes before Workout. The names have to be alphabetically ordered. Don't ask me why, it's just a Rails convention.
So, in this case, you'll do fine with your table being named WorkoutExercises. But if I were you, I'd change it to ExercisesWorkout, just in case, so that you never get it wrong.
Rails Many to Many SQLite3 error
I don't know if it is the reason of your problem, but the has_and_belongs_to_many
association is deprecated.
According to the Rails Guide:
The use of extra attributes on the join table in a has_and_belongs_to_many association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a has_many :through association instead of has_and_belongs_to_many.
I know that you are not adding any extra attribute to the join table, but try changing your migration to the below, which I think is the default:
class CreateChannelPackageJoinTable < ActiveRecord::Migration
def change
create_table :channels_packages, :id => false do |t|
t.integer :channel_id
t.integer :package_id
t.timestamps
end
end
end
Ambiguous column name error when upgrading to Rails 3.1
Here we go...
[In Rails 3.1] Supplying options hash to with_scope, with_exclusive_scope and default_scope has also been deprecated:
default_scope :order => "id DESC"
Try:
default_scope order('categories.id')
Related Topics
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Functional Code Examples in Ruby
How to Push to Faye Server from Rails Controller
Escape Single Quote in Xpath with Nokogiri
Add Space After Commas Only If It Doesn't Already
Can't Install Gems Because "Undefined Method 'Invoke_With_Build_Args' for Nil:Nilclass"
Ruby: Inject Issue When Turning Array into Hash
Use Rvm to Force Specific Ruby in Xcode Run Script Build Phase
Exponentiation in Ruby 1.8.7 Returns Wrong Answers
How to Improve Jruby Load Time
Why Does Array.Each Behavior Depend on Array.New Syntax
New Rails Project: 'Bundle Install' Can't Install Rails in Gemfile
Windows: Rails: Error Installing Bson_Ext
Bundle Install Issue with Libv8 and Rails
Building Ruby with Rbenv and Ruby-Build Fails with Undefined Symbol: Sslv2_Method