纠正多个模型的用户关联

时间:2017-04-05 00:00:19

标签: ruby-on-rails ruby activerecord model filterrific

编辑我仍然遇到显示程序的问题。我有大学'名称显示正确,但由于程序属于大学,我仍然无法正确显示这些名称。用户表有一个program_id,但我还是无法让它工作。

我对Rails相当新,并且遇到了我尚未遇到的情况。

我有三种模式:用户,大学和计划。我希望用户能够选择一所大学和一个课程,但每所大学必须能够拥有多个课程,并且课程必须针对每所大学。 (例如,大学A可能无法提供篮式编织,但可在B大学获得)。我也在使用Filterrific gem,如果这对这个问题很重要的话。

我一直在调查has_one:通过协会,并觉得我95%的方式在那里,但只是不能到达那里!

class User < ActiveRecord::Base
    has_one :university
    has_one :program, through: :university
end

class University < ActiveRecord::Base
    belongs_to :program
    has_many :users, :through => :programs
end

class Program < ActiveRecord::Base
    belongs_to :university
    belongs_to :user
end

我的具体问题是,在显示包含用户特定大学和计划的表格时,我只会获取该ID,而不是该特定大学/计划的名称。此外 - 我不确定这是相关的还是完全分开的 - 但是当我想要编辑用户时,我收到错误ActiveRecord::StatementInvalid at /users/6/edit PG::UndefinedColumn: ERROR: column universities.user_id does not exist

我认为有这个问题是这样的:

<% @users.each do |user| %>
    <tr>
        <td class='center'><%= link_to(user.full_name, user_path(user)) %></td>
        <td class='center'><%= user.email %></td>
        <td class='center'><%= user.role.capitalize %></td>
        <td class='center'><%= user.program_id %></td>    <-- Error
        <td class='center'><%= user.university_id %></td>    <-- Error
    </tr>
<% end %>

用户编辑视图:

<%= form_for(@user) do |f| %>
    <div class="field">
        <%= f.label :full_name %><br />
        <%= f.text_field :full_name %>
    </div>
    <div class="field">
        <%= f.label :email %><br />
        <%= f.email_field :email %>
    </div>
    <div class="field">
        <%= f.label :university %><br>
        <%= f.select :university, options_from_collection_for_select(University.all, 'id', 'name'), {include_blank: true}, {:class => 'select2'} %>
    </div>
    <div class="field">
        <%= f.label :program %><br>
        <%= f.select :program, options_from_collection_for_select(Program.all, 'id', 'name'), {include_blank: true}, {:class => 'select2'} %>
    </div>
    <div class="actions">
        <%= f.submit 'Submit', :class => 'btn btn-trilogy' %>
    </div>
<% end %>

我的用户控制器

class UsersController < ApplicationController
    def index
        @filterrific = initialize_filterrific(
            User,
            params[:filterrific],
            :select_options => {
                sorted_by: User.options_for_sorted_by,
                with_university_id: University.options_for_select,
                with_program_id: Program.options_for_select,
            }
        ) or return
        @users = @filterrific.find.page(params[:page])

        respond_to do |format|
            format.html
            format.js
        end
    end

最后是我的架构

ActiveRecord::Schema.define(version: 20170404205100) do

  enable_extension "plpgsql"

  create_table "programs", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "universities", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "",                    null: false
    t.string   "encrypted_password",     default: "",                    null: false
    t.integer  "role"
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,                     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                                             null: false
    t.datetime "updated_at",                                             null: false
    t.string   "full_name"
    t.integer  "university_id"
    t.string   "time_zone"
    t.integer  "program_id"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

请原谅我,如果您需要查看某些代码缺失。请告诉我,我不会经常在这里发帖!

2.3.3 :006 > university.users
NoMethodError: undefined method `klass' for nil:NilClass
Did you mean?  class
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:537:in `source_reflection'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:728:in `derive_class_name'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:176:in `class_name'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:728:in `derive_class_name'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:176:in `class_name'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/reflection.rb:221:in `klass'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/associations/association.rb:118:in `klass'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/associations/collection_association.rb:41:in `reader'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activerecord-4.1.9/lib/active_record/associations/builder/association.rb:110:in `users'
    from (irb):6
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/railties-4.1.9/lib/rails/commands/console.rb:90:in `start'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/railties-4.1.9/lib/rails/commands/console.rb:9:in `start'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/railties-4.1.9/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:247:in `require'
... 4 levels...
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in `load'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/activesupport-4.1.9/lib/active_support/dependencies.rb:241:in `load'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
    from /Users/jimmiejackson/.rvm/gems/ruby-2.3.3/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/jimmiejackson/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

3 个答案:

答案 0 :(得分:1)

根据您描述项目的方式,您应该更新模型:

class University < ActiveRecord::Base
    has_many :programs
    has_many :users, :through => :programs
end

class Program < ActiveRecord::Base
    belongs_to :university
    has_many :users
end

class User < ActiveRecord::Base
    has_one :program
    has_one :university, through: :program
end

所以你可以按大学的具体计划,如果你想让用户只有一个项目,他将通过该项目获得一所大学。

希望它可以帮助...

答案 1 :(得分:0)

我会在模型之间建议不同类型的关系

class University < ActiveRecord::Base
    has_and_belongs_to_many :programs
    has_many :users
end

class Program < ActiveRecord::Base
    has_and_belongs_to_many :universities
    has_many :users
end

class User < ActiveRecord::Base
    has_one :program
    has_one :university
end

通过这种方式,您可以在声明程序或大学时防止重复数据。在生成的组合表中管理关系。 我们在大学之间建立了多对多的关系,因为它可以有多个项目,一个项目可以由多所大学运行。 这种关系是可扩展的,即大学特定的程序以后可以由其他

打开

用法将是

 user.university
 user.program
 university.programs
 university.users
 program.universities
 program.users

等等。 希望这会有所帮助..

答案 2 :(得分:0)

最终想通了。我发布了我的答案,以防其他人遇到类似的问题。

class University < ActiveRecord::Base
    has_many :programs
    has_many :users, through: :programs
end

class Program < ActiveRecord::Base
    belongs_to :university
    has_many :users, through: :university
end

class User < ActiveRecord::Base
    belongs_to :university
    belongs_to :program
end