0

I can not find the issue with my association, but continuously getting error related to the association. I added has_many to Schools and belongs_to to members.

 class CreateMembers < ActiveRecord::Migration[5.0]
  def change
   create_table :members do |t|
    t.string :name
    t.string :email
    t.timestamps
  end
end

end

 class CreateSchools < ActiveRecord::Migration[5.0]
def change
create_table :schools do |t|
  t.string :name

  t.timestamps
end

end end

 class AddSchoolRefToMembers < ActiveRecord::Migration[5.0]
def change
  add_reference :members, :school, foreign_key: true
end

end

Controller:

class MembersController < ActionController::Base
 before_action :set_school
def index
 @members = Member.all
end
def new
  @member = Member.new
end

def create
 @member = Member.new(member_params)
 @member.school = @school
 @member.save
 redirect_to members_path
end


private

def set_school
 @school = School.find(params[:school])

end

def member_params
  params.require(:member).permit(:name, :email,:school)
end

end

gerazoli
  • 39
  • 8

1 Answers1

0

Instead of assigning the @school itself you should assign the id of that school:

def create
 @member = Member.new(member_params)
 @member.school = @school.id      # here it is @school.id 
 @member.save
 redirect_to members_path
end

The associations work with IDs not Arrays. @school return the school record completely you just need the id to create the association.

Alexander Luna
  • 5,261
  • 4
  • 30
  • 36
  • This is wrong, try this `@member.school = @school` in your console, you can. – ashvin Mar 03 '17 at 05:11
  • As you can read in the rails docs, the proper way of working with associations is through ids: http://guides.rubyonrails.org/association_basics.html I don't work without ids. – Alexander Luna Mar 03 '17 at 05:45
  • Edit: I just tried it in the console and it doesn't work. When I assign a user to a post.user_id, it returns nil and doesn't save. How did it work for you ? – Alexander Luna Mar 03 '17 at 05:49
  • What nil? What you have tried Here is example `a1 = Table1.new(a: 'test', b: 'test') a1.user = User.find(10) a1.save!` – ashvin Mar 03 '17 at 05:53
  • The problem is that a1.user is of type reference and therefore has to be an ID. – Alexander Luna Mar 03 '17 at 06:27
  • We can do using both way, but assign `id` is not necessary. `a1.user = User.find(10).id` or `a1.user = User.find(10)` – ashvin Mar 03 '17 at 06:30
  • Well I tried it in my project and I can't. So something must be different. – Alexander Luna Mar 03 '17 at 06:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137110/discussion-between-ashvin-and-alexander-luna). – ashvin Mar 03 '17 at 06:33
  • I can not chat in there yet due to the reputation, but this still does not work. This is in the new.html.erb: <%= simple_form_for [@school, @member] do |f| %> <%= f.input :name %> <%= f.input :email %> <%= f.select :school, options_for_select(School.all.each{|s|[s.name, s.id ]}) %> <%= f.submit %> <% end %> and I change the ones above, but still get an error message: param is missing or the value is empty: member – gerazoli Mar 03 '17 at 09:58
  • sorry, the error message is : Couldn't find School with 'id'= – gerazoli Mar 03 '17 at 10:07
  • How did you set up the association in your models ? – Alexander Luna Mar 04 '17 at 01:30