Ruby on rails 通过单个文本字段公开多个关系
我有一个模型a“有很多”B 但是Ruby on rails 通过单个文本字段公开多个关系,ruby-on-rails,ruby,accessor,Ruby On Rails,Ruby,Accessor,我有一个模型a“有很多”B 但是def bees=(value)似乎从未被解雇 我做错了什么 编辑2 我的实际代码在这里可见:您可以放置一个:attr\u访问器,如: class A < ActiveRecord::Base has_many :B attr_accessible :title attr_accessor :field_of_happiness def field_of_happiness=(value) # override the setter
def bees=(value)
似乎从未被解雇
我做错了什么
编辑2
我的实际代码在这里可见:您可以放置一个:attr\u访问器,如:
class A < ActiveRecord::Base
has_many :B
attr_accessible :title
attr_accessor :field_of_happiness
def field_of_happiness=(value)
# override the setter method if you want
end
def field_of_happiness(value)
# override the getter method if you want
end
end
class A
它在某些方面对你有帮助吗?哦,天哪。原来问题不在于模型,而在于控制器。。。我忘了在
update
方法中添加一个简单的行来将我的帖子值分配给我的类字段
无论如何,最终的解决方案是:
在我的控制器中:
def update
@a.whatever = params[:a][:whatever]
@a.my_b = params[:a][:my_b]
@a.save
end
在我的模型中:
class A < ActiveRecord::Base
has_many :B
attr_accessible :whatever
def my_b
list = ""
self.B.each do |b|
list += "#{b.name}\n"
end
list
end
def my_b=(value)
# parse the value and save the elements
end
end
class A
我实际上是在测试它。正如我在问题中编辑的那样,二传手似乎从未被解雇过。
def update
@a.whatever = params[:a][:whatever]
@a.my_b = params[:a][:my_b]
@a.save
end
class A < ActiveRecord::Base
has_many :B
attr_accessible :whatever
def my_b
list = ""
self.B.each do |b|
list += "#{b.name}\n"
end
list
end
def my_b=(value)
# parse the value and save the elements
end
end