belongs-to

Validation failed Class must exist

不打扰是莪最后的温柔 提交于 2019-11-27 12:44:30
问题 I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case: City's class: class City < ApplicationRecord has_many :users end User's class: class User < ApplicationRecord belongs_to :city validates :name, presence: true, length: { maximum: 80 } validates :city_id, presence: true end Users Controller: def create Rails.logger.debug user_params.inspect @user = User.new(user_params) if @user.save! flash[:success] = "Works!" redirect

Rails belongs_to many models [closed]

点点圈 提交于 2019-11-27 09:49:59
问题 I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models. Here's the table structure I intend to have: User id Post id user_id #foreign key; a post belongs to a User aka "Who created this post" Comment id user_id #foreign key; a comment belongs to a User aka "Who made this comment" post_id #foreign key; a comment belongs to a Post aka "What post this comment is for" And

Does accepts_nested_attributes_for work with belongs_to?

此生再无相见时 提交于 2019-11-27 07:32:01
I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship? class User < ActiveRecord::Base belongs_to :organization accepts_nested_attributes_for :organization end class Organization < ActiveRecord::Base has_many :users end In a view: = form_for @user do |f| f.label :name, "Name" f.input :name = f.fields_for :organization do |o| o.label :city, "City" o.input :city f.submit "Submit"

Does accepts_nested_attributes_for work with belongs_to?

放肆的年华 提交于 2019-11-26 09:37:08
问题 I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship? class User < ActiveRecord::Base belongs_to :organization accepts_nested_attributes_for :organization end class Organization < ActiveRecord::Base has_many :users end In a view: = form_for @user do |f| f.label :name, \"Name\" f