Ruby on rails 3 Rails 3.1-查找或创建HABTM关系的嵌套属性?

Ruby on rails 3 Rails 3.1-查找或创建HABTM关系的嵌套属性?,ruby-on-rails-3,activerecord,ruby-on-rails-3.1,Ruby On Rails 3,Activerecord,Ruby On Rails 3.1,我的发行版可以有很多艺术家,艺术家可以出现在很多发行版上。艺术家可以通过嵌套属性在我的发布表单中创建。我遇到的麻烦是让find_或create为艺术家工作 我在我的模型中定义了以下代码,正如您可以看到的那样,ArtistRelease模型中有一个相当荒谬的get/set/delete例程,以实现期望的结果。这确实管用,但我不喜欢。我知道有一个更好的方法是通过find_或create。有人能帮忙吗?我应该将“查找”或“创建”放置在何处才能使其工作 class Release < Active

我的发行版可以有很多艺术家,艺术家可以出现在很多发行版上。艺术家可以通过嵌套属性在我的发布表单中创建。我遇到的麻烦是让find_或create为艺术家工作

我在我的模型中定义了以下代码,正如您可以看到的那样,ArtistRelease模型中有一个相当荒谬的get/set/delete例程,以实现期望的结果。这确实管用,但我不喜欢。我知道有一个更好的方法是通过find_或create。有人能帮忙吗?我应该将“查找”或“创建”放置在何处才能使其工作

class Release < ActiveRecord::Base
  has_many :artist_releases, :dependent => :destroy
  has_many :artists, :through => :artist_releases
  accepts_nested_attributes_for :artists, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
  accepts_nested_attributes_for :artist_releases
end

class Artist < ActiveRecord::Base
  has_many :artist_releases
  has_many :releases, :through => :artist_releases
end

class ArtistRelease < ActiveRecord::Base
  belongs_to :artist
  belongs_to :release

  before_create :set_artist_id
  after_create :destroy_artist

  default_scope :order => 'artist_releases.position ASC'

  private    
  def set_artist_id
    a = Artist.where("name =?", artist.name).reorder("created_at").find(:first)
    a.role = artist.role
    a.position = artist.position
    a.save
    artist_id = a.id
    self.artist_id =  artist_id
  end

  def destroy_artist
    c = Artist.count(:all, :conditions => [ "name = ?", artist.name])
      if c > 1
        a = Artist.where("name =?", artist.name).reorder("created_at").find(:last)
        a.destroy
      end
    end
end
类发布:destroy
有很多:艺术家,:至=>:艺术家
接受_嵌套的_属性_for:artists,:reject _if=>lambda{a | a[:name].blank?},:allow _destroy=>:true
接受\u嵌套的\u属性\u用于:艺术家\u发布
结束
类艺术家:艺术家发行版
结束
类ArtistRelease“Artister\u releases.position ASC”
私有的
def设置\u艺术家\u id
a=艺术家。其中(“名称=?”,艺术家。名称)。重新排序(“创建位置”)。查找(:第一个)
a、 角色
a、 位置=艺术家位置
a、 拯救
艺术家id=a.id
self.artist\u id=artist\u id
结束
艺术家
c=Artist.count(:all,:conditions=>[“name=?”,Artist.name])
如果c>1
a=艺术家。其中(“名称=?”,艺术家。名称)。重新排序(“创建位置”)。查找(:last)
a、 毁灭
结束
结束
结束

您似乎正在寻找[model\u name]的
自动保存关联的\u记录\u
方法,您应该在其中一个模型中覆盖该方法

这里有解释:。这是一个简单的
关联的例子

因为
有很多:通过
关联,我在下面的代码中开发了类似的东西

在我的应用程序中,我有一个
订单项目----客户端连接。我正在使用新的订单项目表单创建客户。解决方案允许我创建一个新的客户端和席位关联,或者当客户端表中已经有一个客户端提供了电子邮件时,仅创建席位关联。它不会更新现有客户机的其他属性,但也可以轻松地进行扩展

class OrderItem < ActiveRecord::Base
  attr_accessible :productable_id, :seats_attributes

  has_many :seats, dependent: :destroy
  has_many :clients, autosave: true, through: :seats

  accepts_nested_attributes_for :seats, allow_destroy: true   
end

class Client
  attr_accessible :name, :phone_1
  has_many :seats
  has_many :order_items, through: :seats
end

class Seat < ActiveRecord::Base
  attr_accessible :client_id, :order_item_id, :client_attributes

  belongs_to :client, autosave: true
  belongs_to :order_item

  accepts_nested_attributes_for :client

  def autosave_associated_records_for_client
    if new_client = Client.find_by_email(client.email)
      self.client = new_client
    else
      # not quite sure why I need the part before the if, 
      # but somehow the seat is losing its client_id value
      self.client = client if self.client.save!
    end
  end
end
class OrderItem
希望有帮助