开发者

Why is the link not being generated here?

开发者 https://www.devze.com 2023-03-02 05:30 出处:网络
I have these two methods in my signeduser model: def build_invitation self.create_invitation end def generate_url

I have these two methods in my signeduser model:

def build_invitation
  self.create_invitation
end

def generate_url
  self.invitation.invitation_url = "http://www.dreamstill.com/?id=#{self.invitation.id}"
end

I also have a custom rake task that calls these two methods:

task :generate_url => :environment do
  SignedUser.all.each do |user|
    user.build_invitation
    user.generate_url
  end
end

It seems that although the invitations were create, the urls were not generated for the invitation. Maybe it开发者_StackOverflow中文版's because I did not save it? How do I fix the method?


It looks like you're forgetting to save the result of your assignment and it's being lost when the object is discarded.

task :generate_url => :environment do
  SignedUser.all.each do |user|
    user.build_invitation
    user.generate_url
    user.save(false)
  end
end

Passing false as an argument to save avoids running the validations and basically forces a save. This may help with situations where the user record is invalid, such as new requirements being imposed on an old record, for some reason but you want to save this one change anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消