Tag: 模型 关联

Rails:在属于其他两个模型的模型中创建一个新条目

考虑一个has_many产品的商店,其中有很多意见。 这是模特: 商店: class Store :products end 产品: class Product < ActiveRecord::Base attr_accessible :desc, :name, :status, :url belongs_to :store has_many :opinions end 最后,意见: class Opinion < ActiveRecord::Base attr_accessible :content, :eval, :status belongs_to :store belongs_to :product end 要创建新的意见(属于产品和商店),这里是OpinionsController的创建方法: def create # Get product info. product = Product.find_by_id params[:opinion][:product_id] # Create a new opinion to this product […]

has_one嵌套属性不保存

我有两个模型Project和ProjectPipeline。 我想创建一个Project表单,该表单也包含ProjectPipeline模型中的字段。 我已成功创建表单但是当我点击保存时,值不会存储在数据库中。 project.rb class Project < ActiveRecord::Base has_one :project_pipeline accepts_nested_attributes_for :project_pipeline self.primary_key = :project_id end projectpipeline.rb class ProjectPipeline < ActiveRecord::Base belongs_to :project, autosave: :true validates_uniqueness_of :project_id end 我并不总是想要一个项目管道,但是在适当的条件下,基于用户查看项目。 我希望构建项目管道字段,但只有在用户选择保存/填充它们时才会保存。 因此,当项目显示时,我使用project_id构建一个临时项目管道:来自params [:id](不确定我是否真的需要这样做)。 然后,当项目保存时,我使用create_attributes。 但是如果它已经被创建或构建,我只想让has_one和belongs_to关联启动,然后使用update_attributes。 我的问题是当我试图保存时,如果我使用params [:project_pipeline]或者如果我使用project_params则根本没有保存,我要么遇到’Forbidden Attribute’错误。 我已经检查并重新检查了我在project_params中的所有字段,甚至尝试使用project_pipeline_params,但感觉不对。 这让我疯了,我需要睡觉。 projects_controller.rb def show @project = Project.find(params[:id]) if @project.project_pipeline else @project.build_project_pipeline(project_id: params[:id]) end autopopulate end def update […]

获得至少一个关联对象的记录

我在mongoid中有以下架构: 用户有很多任务 – has_many:tasks 任务属于user – belongs_to:user 如何只有10个第一个用户至少有一个任务? 像这样的东西: User.where(:tasks.ne => [] ).limit(10)

Ruby on Rails:具有多个外键的模型关联

我正在研究User模型,每个用户都应该能够同时拥有学生和教师。 但是,由于学生和老师都是User类型,我的模型有点复杂。 这就是我现在正在尝试的。 Teacher_student_link class TeacherStudentLink “User” belongs_to :student, :class_name => “User” end 用户 class User { :student_id, :teacher_id }, :dependent => :destroy has_many :students, :through => :teacher_student_links has_many :teachers, :through => :teacher_student_links end 如果一切按照我的意图行事,我应该能做到 @user = User.new @user.students @user.teachers @user.student.teachers 我认为上面唯一的问题是我不能同时给teacher_student_link两个外键,但我不确定。 作为一种解决方法,我的模型中还没有teacher_id,只是让student.user给teacher打电话。 任何人都可以帮我解决这个问题吗? 更新:通过以下解决方案,我应该如何创建新链接? def become_student @user = user.find(params[:id]) @student_link = @user.student_links.create(:student_id => current_user.id) […]

Rails 3 – has_and_belongs_to_many

我有2个模特 – 老师和主题 。 想要通过名称Qualification连接表连接它们。 看起来我做错了什么: class Teacher “Qualification” end class Subject “Qualification” end 我的迁移: class CreateQualificationJoinTable false do |t| t.integer :subject_id t.integer :teacher_id end add_index :qualification, :subject_id add_index :qualification, :teacher_id end end 例如,当我打开rails console并打印时 ruby-1.9.3-head :013 > Qualification 我明白了: NameError: uninitialized constant Qualification from (irb):13 from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start’ from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start’ from /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/railties-3.2.0/lib/rails/commands.rb:41:in `’ […]