从其他模型更改数据

时间:2012-07-26 16:47:04

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1

我想知道我应该接受什么样的查询来允许我的数据更新。我的模型包括客户,兴趣和经理

Clients his has follow
id
name
email
password

Interest
id
description

manager
customer_id
interest_id
created_at

经理的目标是他不要覆盖感兴趣的旧数据,而只是不断添加新兴趣并引用它。

The relationship his has follow
class Client < ActiveRecord::Base
  has_many :music_interest_managers
  has_many :music_interests, through => :music_interest_managers
end
class MusicInterest < ActiveRecord::Base
  has_many :music_interest_managers
  has_many :clients, through => :music_interest_managers
end
class MusicInterestManager < ActiveRecord::Base
  belongs_to :music_interests
  belongs_to :client
end

现在要更新客户控制器的数据我不知道如何做到这一点 这就是我在想的:

@client = Client.find(params[:id])
@manager = @client.manager.build(params[:manager])
@interest = @interest.manager.build(params[:interest])

这有意义吗?或者我错了?

更新

def update
    @client = Client.find(params[:id])
    @interest = @client.music_interests.build(params[:interest])

    if @client.update_attributes(params[:client])
        flash[:success] = "Profile updated"
        #sign_in @client
        redirect_to @client
    else
        render 'edit'
    end
end

或者我应该从感兴趣的角度渲染模型视图然后应用更改?

1 个答案:

答案 0 :(得分:0)

@client = Client.find(params[:id])
@interest = @client.music_interests.build(params[:interest])

应该可以工作 - 在控制台中试试!