Rails Association error
You need to enable nested attributes for :occasions
in your model:
class User ActiveRecord::Base
has_many :occasions
accepts_nested_attributes_for :occasions
end
Otherwise the form builder does not not how to use the Occasion
object that you're building in the controller, so it'll just use the :occasions
symbol as "object", giving you this error.
rails error Class must exist - Associations
Rails 5 makes belongs_to
association required by default. So you cannot create Post
without associating it with a Travel
@post = Post.new(post_params)
@post.travel = travel
@post.save
If you want to make the association optional, you will have to mention it explicitly
class Post < ActiveRecord::Base
belongs_to :travel, optional: true
end
Rails 4: has many through association error
Rails convention expects your through table to be named: collections_releases
. And your model CollectionsRelease
. Of course you could override if you wanted to keep the singular collection_releases
name.
Rails 3 'association :[...] not found' error for has_many :through relationship
If you are working over PressRelease
you should use <%= f.association :categories %>
, change the :category
for :categories
, because of the relation.
Show association errors in a form
When you make the call to redirect_to product_path(@product)
, you are invoking a different controller action, namely the Products#show
action. The instance variables your views will have been reset. You probably aren't initializing a @review
instance variable in this action which is why you are getting an exception.
What you want is something like this
# posts_controller.rb
def show
@product = Product.find(params[:product_id])
@review = @product.reviews.build
end
# show.html.erb
= form_for([@product, @review]) do |f|
-if @review.errors.any?
.errors
%h2
=pluralize(@review.errors.count, "error")
%ul
=@review.errors.full_messages.each do |msg|
%li
=msg
# reviews_controller.rb
def create
@product = Product.find(params[:product_id])
@review = @product.reviews.build(params[:review])
if @review.save
redirect_to product_path(@product)
else
render 'products/show'
end
end
The key is that if the review
is unsuccessfully created, you will re-render the page with the same instance variables intact.
User has_many association not working (error:Could not find the association :user_categories in model Category)
I can't say for sure what the problem could be, but a few things I could point out:
UserCategory's accepts_nested_attributes_for, does that mean the you want to be able to dynamically create categories?
Category has_many :users, through: :user_categories, not user
You need to follow the Rails file naming conventions, user.rb, user_category.rb and category.rb
These may not be the problem/solution, but I believe they're in the way of resolving the problem.
Rails - belongs_to has_many association error
- Is a belongs_to / has_many association correct for this situation? Should this be a has_one?
To setup a relationship where a user can only belong to a single group but groups have many users then you are on the right track.
class Group < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :group
end
belongs_to
places the foreign key column as users.group_id
. Using has_one
would place it on groups.user_id
column which would only allow a one to one mapping - a group could only have a single user.
- Should both migrations have a foreign key attribute?
No - only the the users
table should contain a foreign key. The AddUserReferenceToGroup
should be removed or you should write another migration which removes the groups.user_id
column if you have pushed it to production.
- Is setting
@group.user_id = current_user.id
inGroupsController#create
a suitable way to assign the creating user
to the group?
No - since the group_id
column is on the users
column you need to update the users table - not groups.
if @group.save
current_user.update(group: @group)
end
- I would also like to enforce (at the database level) that a user can only belong to one group - Is this achieved using unique => true in the schema?
No - since a row on the users
table can only have one id in the groups_id
column a user can only belong to one group anyways. Using unique => true
would create a uniqueness index on the users.groups_id
column which would only allow a single users to be associated with a group.
- How can I enforce (at the database level) that a group must have a user?
This is not actually possible. To be able to associate a user with a group you must first insert the group into the database so that it is assigned a id.
Adding a validation on the software level would also create a "chicken vs egg" situation where a group cannot be valid since it has no users and a user cannot be associated with a group since it is not persisted.
You can however setup a constraint on the belongs_to
end by declaring the foreign key column as NOT NULL
.
Ruby on Rails Model method error with has_many associations(revised)
As seen in the teams controller above, the method used in the create
action was "@team = @scoreboard.teams.build(team_params)". This method
should create a has_many association between scoreboard and teams.
However, the problem is that I get an error "Undefined method "teams"
for nil class"
You haven't initialized @scoreboard
, so @team = @scoreboard.teams.build(team_params)
doesn't work. Defining it like @scoreboard = Scoreboard.find(params[:scoreboard_id])
should solve your problem.
Also, how do we know that teams are related to that specific
scoreboard. For example, the user could have many scoreboards. How do
we make sure that the primary key of that specific scoreboard is the
foreign key of the teams
With the foreign_key scoreboard_id
available in teams
table you can get the teams associated with specific scoreboard by calling @scoreboard.teams
.
For example, assume you have the following code
@scoreboard = Scoreboard.find(1)
@scoreboard.teams #will return the teams associated with it.
Also I recommend you to check these guides for your better understanding on how the things work behind the scenes.
Related Topics
Assets Precompiling Error with Jquery UI Plugin
Ruby, Value Bucketing, Beautify Code
Ruby Webrick Http Authentication
Require Self Made Gem in Jruby Fails After Update to Jruby-1.7.13
How to Fix My Cucumber Expectation Error When Polling
Linking to External File in Ruby on Rails
Module and Class with the Same Name in Rails Project
Determining Type of an Object in Ruby
Implicit User Creation with Authlogic and Authlogic Oauth Plugin
How to Understand Strptime VS. Strftime
Can't Run a Ruby Hello World Application in Aptana
Replacing an Element in Nested Array Ruby
If Referer = "/Search", Do X, Else Do Y: "Bad Argument (Expected Uri Object or Uri String)"
Why Doesn't This Code Produce the Desired Result
How to Set Up These Crud Controller Actions for Has_Many_Polymorphs and an Error
What Does the |Variable| Syntax Mean