相关疑难解决方法(0)

在rails中使用带有has_one关联的构建

在这个例子中,我创建了一个user没有profile,然后在profile为该用户创建一个.我尝试使用带有has_one关联的构建,但是爆炸了.我看到这个工作的唯一方法是使用has_many.本user应该只有最多只能有一个profile.

我一直在尝试这个.我有:

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

但当我这样做时:

user.build_profile 
Run Code Online (Sandbox Code Playgroud)

我收到错误:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4)  LIMIT 1
Run Code Online (Sandbox Code Playgroud)

在rails中有一种方法可以有0或1个关联吗?

ruby-on-rails database-relations associations has-one

136
推荐指数
2
解决办法
7万
查看次数

accepted_nested_attributes_for with has_many =>:通过选项

我有两个模型,链接和标签,通过第三个link_tags关联.以下代码位于我的链接模型中.

协会:

class Link < ActiveRecord::Base
  has_many :tags, :through => :link_tags
  has_many :link_tags

  accepts_nested_attributes_for :tags, :allow_destroy => :false, 
  :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Tag < ActiveRecord::Base
  has_many :links, :through => :link_tags
  has_many :link_tags
end

class LinkTag < ActiveRecord::Base
  belongs_to :link
  belongs_to :tag
end
Run Code Online (Sandbox Code Playgroud)

links_controller动作:

  def new
    @link = @current_user.links.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @link }
    end
  end

  def create
    @link = @current_user.links.build(params[:link])

    respond_to do …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through nested-attributes

26
推荐指数
3
解决办法
3万
查看次数

Rails has_many:通过嵌套表单

我刚刚加入has_many :through联盟.我试图实现保存所有3个表数据(的能力Physician,Patient通过一个单一的形式和关联表).

我的迁移:

class CreatePhysicians < ActiveRecord::Migration
  def self.up
    create_table :physicians do |t|
      t.string :name
      t.timestamps
    end
  end
end

class CreatePatients < ActiveRecord::Migration
  def self.up
    create_table :patients do |t|
      t.string :name
      t.timestamps
    end
  end
end

class CreateAppointments < ActiveRecord::Migration
  def self.up
    create_table :appointments do |t|
      t.integer :physician_id
      t.integer :patient_id
      t.date :appointment_date
      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的模特:

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
  accepts_nested_attributes_for :appointments
  accepts_nested_attributes_for :physicians
end
class Physician …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms has-many-through

22
推荐指数
3
解决办法
2万
查看次数

accepted_nested_attributes_for通过连接表与连接上的属性

在为连接表添加属性时,如何在has_many:through关联中使用ActiveRecord的accepts_nested_attributes_for帮助程序?

例如,假设我有一个团队模型:

class Team < ActiveRecord::Base
  role = Role.find_by_name('player')
  has_many  :players,
            :through    => :interactions, 
            :source     => :user, 
            :conditions => ["interactions.role_id = ?", role.id] do
              class_eval do
                define_method("<<") do |r|                                                             
                  Interaction.send(:with_scope, :create => {:role_id => role.id}) { self.concat r }
                end
              end
            end
end
Run Code Online (Sandbox Code Playgroud)

团队已经players完成了interactions,因为用户可以占据多个角色(玩家,经理等).

如何在连接表中添加属性的同时使用accepts_nested_attributes_for?

如果我有现有的团队记录team和现有的用户记录user,我可以这样做:

team.players << user
team.players.size 
=> 1
Run Code Online (Sandbox Code Playgroud)

但是如果我用嵌套播放器创建一个新团队:

team = Team.create(:name => "New York Lions", 
                   :players_attributes => [{:name => 'John Doe'}])
team.players.size
=> 0
Run Code Online (Sandbox Code Playgroud)

在最后一个示例中,创建了团队,用户和交互记录(团队确实让用户通过交互),但此处未设置interact.role_id属性.

activerecord ruby-on-rails

5
推荐指数
1
解决办法
1940
查看次数

我是否需要has_many的联接表:通过关联?

我之前只创建了一个has_and_belongs_to_many关联,它与has_many:through不同.对于has_many:通过关联,我需要一个连接表吗?实际协会如何运作?我需要索引吗?我找不到一个很棒的教程,有什么建议吗?

ruby-on-rails associations has-many has-many-through

4
推荐指数
1
解决办法
8511
查看次数

Rails4中的嵌套简单表单 - 有很多通过,保存多个记录

通过关系,我有一个标准的has_many.人类通过连接表交互有许多兽人.互动只是一个表格和模型; 没有控制器或视图.

使用Rails 4中的simpleform gem,我想从人体页面创建一个表单,以便从所有兽人的池中选择多个兽人.提交后,我希望它在交互表中创建/更新尽可能多的记录,每个记录都包含人工ID,并且选择了多个orc ID.:

AKA列表符号

  1. 从一端制作表格(人类)
  2. 列出表格中的所有兽人
  3. 从该列表中选择多个兽人
  4. 将多个记录保存到交互表中,human_id并且orc_id从该列表中选择兽人.(human_id在这些记录中将是相同的,因为它从给定的人类表单页面开始)

我将尽可能多地编写整个故事的代码.请随时要求澄清,并解决任何错误,以实现这一点.

humans
  integer "id"

interactions
  integer "human_id"
  integer "orc_id"


  index ["human_id", "orc_id"] 
  # This is the primary key. no normal id.
  # Is it better to have a primary id for this join table, or does it not matter?

orcs
  integer "id"
Run Code Online (Sandbox Code Playgroud)

楷模

/models/human.rb

class Human < ActiveRecord::Base
  has_many :interaction
  has_many :orcs, through: :interactions

  accepts_nested_attributes_for :interactions
end
Run Code Online (Sandbox Code Playgroud)

/models/interaction.rb

# Purely a join model and …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms has-many-through simple-form

3
推荐指数
1
解决办法
1629
查看次数