我有一个名为Discussion的简单模型,它有一个名为resolved的布尔列.
在我的表单中,我有以下代码
<%= form_for(@discussion) do |d| %>
...
<%= d.check_box :resolved %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有以下内容:
def update
@discussion = Discussion.find(params[:id])
if @discussion.update_attributes(params[:discussion])
etc...
end
end
Run Code Online (Sandbox Code Playgroud)
当我提交表单时,我可以看到参数被发送到服务器...
Parameters: {"utf8"=>"?", "authenticity_token"=>"AsGsRHwiVva/+kTrBs0IjLeZwj1ZmXBuKZr9Pg/N6Xk=", "discussion"=>{"shortdesc"=>"Talk about something.", "content"=>"Try to update check box.", "resolved"=>"1"}, "commit"=>"Update Discussion", "id"=>"1"}
Run Code Online (Sandbox Code Playgroud)
但该查询不包含有关更新该字段的任何内容.
AREL (14.9ms) UPDATE "discussions" SET "content" = 'Try to update check box.', "updated_at" = '2011-07-18 17:53:50.783176' WHERE "discussions"."id" = 1
Run Code Online (Sandbox Code Playgroud)
对我缺少什么有任何想法?
我希望能够有一个链接按下时根据控制器动作更新一个属性.我怎么能这样做?我是否必须创建单独的更新路由?我是新来的,所以请耐心等待.
调节器
def completed
@meeting = Meeting.find(params[:id])
if @meeting.day_of_meeting == @meeting.days
@meeting.update_attribute(:day_of_meeting, '1')
redirect_to :back
else
@meeting.increment!
redirect_to :back
end
end
Run Code Online (Sandbox Code Playgroud)
模型
def increment!
update_attribute(:day_of_meeting, day_of_meeting + 1)
end
Run Code Online (Sandbox Code Playgroud)
视图
<%= link_to meetings_completed_path(@meeting), remote: true do %>
Run Code Online (Sandbox Code Playgroud)
路线
resources :meetings do
get 'meetings/completed'
end
Run Code Online (Sandbox Code Playgroud) 从根本上说,我想做一个ActiveRecord"update_attributes",同时让ActiveRecord更改为待处理.有没有办法实现这个目标?如果你想知道为什么我会想要这个,请继续阅读.
我有一个由三部分组成的表单:静态部分(彼此独立的文本字段),一组选择(填充条目时增长),以及显示选择对一组依赖对象的影响的部分.更改选择需要往返服务器以确定效果,并且某些选择会影响将来选择的选择.选择被建模为基本模型的has_many关联.例如(注意[]条目指定HTML-SELECTs)
Include [section]
Exclude [subsection]
Exclude [subsection]
Include [section]
Exclude [subsection]
...
Run Code Online (Sandbox Code Playgroud)
我已将表单设置为典型的嵌套属性表单.当选择被更改时,我使用AJAX回发字段,以便获得典型的params哈希值(例如params [:basemodel] [associated_models_attributes] [0] [:field_name]).我想把它变成一个未保存的ActiveRecord,这样我用来生成原始页面部分的部分就可以用来生成JS响应(我正在使用js.erb文件).使用Basemodel.new(params [:basemodel])给出错误
"ActiveRecord::RecordNotFound (Couldn't find AssociatedModel with ID=1 for Basemodel with ID=)
Run Code Online (Sandbox Code Playgroud)
这种情况正在发生(我认为),因为现有关联记录(当前记录中包含非空ID)的ID与从"新"调用生成的空白ID不匹配.
我可以做一些真正的kludgy并创建一些看起来像ActiveRecord的东西(至少足以满足我的偏见)但我必须认为这是一个很常见的问题,它有一个很好的解决方案.
activerecord ruby-on-rails associations update-attributes ruby-on-rails-3
我意识到以下几点:
user1 = User.create - creates a model with id 1 and age is 0
same_user = User.find(1)
same_user.update_attributes(:age => 18)
p same_user.age # prints out 18
p user1.age # prints out 0
Run Code Online (Sandbox Code Playgroud)
为什么现有属性不从数据库中获取值?
在此之前,我想感谢你的帮助
我有这样的模型:
attr_protected nil
belongs_to :product
belongs_to :user
before_create :add_ammount
def carted_product_price(ammount, price)
ammount * price
end
def add_ammount
carted_product = CartedProduct.where(:product_id => self.product_id, :user_id => self.user_id)
if carted_product
carted_product.first.ammount += self.ammount
carted_product.first.update_attributes(:ammount => carted_product.first.ammount)
else
self.save
end
end
Run Code Online (Sandbox Code Playgroud)
它将购买订单保存在名为Carted_Products的表中,该表连接到belogings中的用户和产品
问题是,当Before create执行时,我希望它更新表中的记录,添加控制器传递的ammount,如果记录已经存在,如果没有,创建一个,就iv完成,它更新ammount但是STILL使用顺序中传递的参数创建一个新的,我希望它只更新,而不是在找到记录时执行这两个操作
thnx耐心等待
编辑:
尝试在更新属性后返回false,它取消过滤器,并且不创建或更新属性
我正在一个用户可以拥有的项目中工作admin: true / false.该应用程序只允许管理员用户登录系统,其他用户只是其设置只有管理员可以编辑的客户端.这是某种商业应用程序.(Rails 3.2.13)
我想将这两个概念保存在同一个表中,因为将来可能会选择登录非管理员用户并与他们的配置文件进行交互,因此所有逻辑都已经实现.
我有这个User资源:
ActiveRecord::Schema.define(:version => 20131204200554) do
create_table "users", :force => true do |t|
t.string "name"
t.string "surname"
t.boolean "admin"
t.string "password_digest"
t.string "email"
t.string "auth_token"
end
end
Run Code Online (Sandbox Code Playgroud)
这是用户模型:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :name, :surname,:email, :password, :password_confirmation
validates :name, presence:true, length: { maximum:50}
validates :first_surname, presence:true, length: { maximum:50}
VALID_EMAIL_REGEX= /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive:false}
validates :password, length:{minimum:6}
validates :password_confirmation, presence:true …Run Code Online (Sandbox Code Playgroud) 我正在制作一个博客应用程序。我需要根据已更改的属性数量有两种不同的方法。本质上,如果仅发布日期发生变化,我会做一件事......即使发布日期和其他任何事情发生变化,我也会做另一件事。
posts_controller.rb
def special_update
if #detect change of @post.publication_date only
#do something
elsif # @post changes besides publication_date
elsif #no changes
end
end
Run Code Online (Sandbox Code Playgroud) 问题很简单,update_attributes是否验证了模型的每个可能的验证,即使我不想更新某些属性?
我有一个编辑视图,用户可能会在其中更改密码,但只有在他通过密码时才会更改密码,即如果密码不会更改.
所以我在控制器中执行以下操作:
def update
params[resource_name].delete(:password) if params[resource_name][:password].blank?
params[resource_name].delete(:password_confirmation) if params[resource_name][:password_confirmation].blank?
params[resource_name].delete(:current_password) if params[resource_name][:current_password].blank?
if resource.update_attributes(params[resource_name])
...
end
end
Run Code Online (Sandbox Code Playgroud)
我在模型上定义了以下验证:
validates :password,
:length => { :within => 6..40 }
Run Code Online (Sandbox Code Playgroud)
因此,每当我使用调用更新时,我都会收到一条错误消息,指出密码太短
Ps.:我正在使用Devise来解决这个问题.
编辑:你们有没有人知道如果Devise已经对密码进行了任何验证?因为,我删除了验证,它以正确的方式工作,但如果我输入一个简短的密码,它仍然显示验证,说它太短.
我有一个状态报告,用户可以通过电子邮件发送该报告,我想在完成传递操作后将:sent_mail列更新为true。
def send_status
date = Date.today
reports = current_user.reports.for_date(date)
ReportMailer.status_email(current_user, reports, date).deliver
reports.update_all(sent_mail: true)
end
Run Code Online (Sandbox Code Playgroud)
和表类
AddSentMailToReports < ActiveRecord::Migration
def change
add_column :reports, :sent_mail, :boolean, default: false
end
end
Run Code Online (Sandbox Code Playgroud)
但是,在控制台中,send_mail仍然设置为false。有什么想法为什么不起作用?谢谢!
我有一个典型的Post模型:
class Post< ActiveRecord::Base
validates_presence_of :user_id #Line 1
validates_presence_of :title,:body #Line 2
Run Code Online (Sandbox Code Playgroud)
在控制器中,我有:
def create
if request.post?
if login_required
@post = Post.new(params[:post]) #Line 3
@post .update_attribute("user_id",session[:userid]) #Line 4
Run Code Online (Sandbox Code Playgroud)
但是,如果第2行上的验证失败,仍将创建帖子,除非注释掉第4行.
1)为什么?
2)修复建议?
谢谢
activerecord ×1
associations ×1
attributes ×1
boolean ×1
controller ×1
devise ×1
self ×1
updates ×1
validation ×1