I have a model Messages, for which I have a recipient_list which saves as a string. For whatever reason on save, all of my parameters other than the recipient_list are being saved, with only the recipient_list being left out. I'm stumped as to what the cause for this may be.
Model:
class Message < ActiveRecord::Base
attr_accessible :content, :sender_id, :recipient_list
attr_reader :recipient_list #necessary for jquery-token-input
belongs_to :sender, class_name: "User"
validates :content, presence: true
validates :sender_id, presence: true
validates :recipient_list, presence: true
def recipient_list=(recipient) #jquery-token-input
self.recipient_ids = recipients.split(",")
end
end
Controller:
def create
#message = current_user.sent_messages.build(params[:message])
if #message.save
flash[:success] = "Message Sent."
redirect_to '/users/'+current_user.id.to_s+'/messages'
else
redirect_to '/users/'+current_user.id.to_s+'/messages'
end
end
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"WlStV4ogguSX72vrZp10zJbucS5MTL1pT1DLt06qjcw=",
"message"=>{"recipient_list"=>"1,2",
"content"=>"foobar123",
"sender_id"=>"1"},
"commit"=>"Send"}
Result:
#<Message id: 32, content: "foobar123", sender_id: 1, recipient_list: "", created_at: "2012-08-22 19:38:44", updated_at: "2012-08-22 19:38:44">]
What might be the problem that is keeping the recipient_list from being saved in this case?
Edit:
Par Ylan's note I set out to see why it was working despite the difference in variable name.
upon messing with it, I realized that it actually was only working that way if i made recipient -> recipients or the reverse the it would stop working.
Fiddled with it, and based on Nash's suggestion came up with the following:
def recipient_list=(ids)
recipient_list = ids.split(",")
super(recipient_list)
end
#<Message id: 42, content: "foobar123", sender_id: 1, recipient_list: "---\n- '1'\n", created_at: "2012-08-22 21:58:46", updated_at: "2012-08-22 21:58:46">]
So now the recipient_list is being saved, I just have to figure out how to remove all the unecessary garble and get just the '1' lol. Any further suggestions?
Edit #2:
After adding
serialize :recipient_list, Array
#<Message id: 43, content: "foobar123", sender_id: 1, recipient_list: ["1", "2"], created_at: "2012-08-22 22:10:46", updated_at: "2012-08-22 22:10:46">]
is the new out put which is what i was going for. We worked together on this one. Thanks you two.
looks like you should call super method in your overriden writer:
def recipient_list=(recipients) #jquery-token-input
self.recipient_ids = recipients.split(",")
super(recipients)
end
or something similar depends on your code.
I believe you have a typo in your writter method. You are passing an argument named recipient, but call recipients.split(","). Change either one and you should be set.
Related
I'm trying to create a callback for a Post model. The callback should update the slug post attribute. This is the relevant code in post.rb:
class Post < ApplicationRecord
after_create :set_slug
SLUG_FILTER = '/\!?##$`\'%^&*+=",.()[]{}‘’'
private
def set_slug
self.slug = title.delete(SLUG_FILTER).strip.gsub(/\s+/,'-').downcase
end
end
When I create a Post in rails console I get this output:
irb> author(1).posts.create!(title: 'some post title', markdown: 'test **123**')
=> #<Post id: 25, title: "some post title", markdown: "test **123**", ..., slug: "some-post-title">
irb> Post.find(25)
=> #<Post id: 25, title: "some post title", markdown: "test **123**", ..., slug: nil>
It seems that the slug attribute is created but then it becomes nil. Why is the slug attribute nil and not some-post-title? I'd appreciate any help.
self.slug = is just assign slug variable so not save to database.
So you should like the below code.
before_create :set_slug
This code assign to slug before writing to database.
the after_create callback is trigger after the object was saved. If you modify an attribute it will not be save again.
Before_create should work :)
I have on my Message model, an after_create which creates a new instance of a Notification like such.
after_create :send_notification
def send_notification
n = Notification.new :name => "#{self.sender.smart_name} sent you a message:", :user_id => self.receiver_id, :notification_type => 'message', :subject => self.subject
n.save
end
However, the objects that are created all have their created_at and updated_at set to nil.
#<Notification:0x0000000c486208
id: 123123,
user_id: 3423,
name: "I sent you a message:\n" + "10:27",
notification_type: "message",
created_at: nil,
updated_at: nil>
I've checked to see that the model.record_timestamps is set to true based on this answer.
I don't have anything set on active_record as suggested here.
I'm using Mysql on Rails 4.
You should call n.reload after n.save just to get the timestamps read after save
I ran the code #transaction = Transaction.new Then I gave it some values:
<Transaction id: nil, debit_uri: "d8hmFJ89CIQUZMBoiPMnvWkQJW/bank_...", credit_uri: "d8hmciqLOg9bCIQUZMBoiPMnvWkQJW/cards...", seller_id: 2, buyer_id: 6, product_id: 31, price: #<BigDecimal:b4a6115c,'0.45E2',9(36)>, ship_price: #<BigDecimal:b4a61094,'0.123E3',9(36)>, ship_method: "fedex", created_at: nil, updated_at: nil>
but when I do #transaction.save! bang(!) or not I get the error:
NoMethodError: undefined method `clear' for nil:NilClass
from /home/alain/.rvm/gems/ruby-1.9.3-head/gems/activemodel-3.2.13/lib/active_model/validations.rb:194:in `valid?'
so I don't know where to look for the error being how my model has little to nothing and there is no method called clear.
class Transaction < ActiveRecord::Base
attr_accessible :buyer_id, :credit_uri, :debit_uri, :price, :product_id, :seller_id, :ship_method, :ship_price
require 'balanced'
attr_reader :errors
end
Regarding to Rails codebase, the error comes from:
attr_reader :errors
Look here. Try to remove it from your model.
Why?
Since you override the errors attributes and did not set it when creating your transaction instance, Rails is trying to do:
nil.clear
I have a standard model with a few fields that are saved to a DB, and I need 1 field that doesn't have to be saved.
I tried attr_accessor but that doesn't cover it. Using Attr_accessor I can set and get the field, but it is not part of the model. If I add the models to an array and then see what is in the virtual field is not part of it. I also tried to add the field :headerfield to attr_accessible but that didn't change anything.
How can I get a field that is part of the model but not saved to the database?
The model
class Mapping < ActiveRecord::Base
attr_accessible :internalfield, :sourcefield
attr_accessor :headerfield
end
console output:
1.9.3-p194 :001 > m = Mapping.new
=> #<Mapping id: nil, internalfield: nil, sourcefield: nil, created_at: nil, updated_at: nil, data_set_id: nil>
1.9.3-p194 :002 > m.headerfield = "asef"
=> "asef"
1.9.3-p194 :003 > m
=> #<Mapping id: nil, internalfield: nil, sourcefield: nil, created_at: nil, updated_at: nil, data_set_id: nil>
Because ActiveRecord::Base has custom implementations for the standard serializiation methods (including to_s and as_json), you will never see your model attributes that do not have backing database columns unless you intervene in some way.
You can render it to JSON using the following:
render json: my_object, methods: [:virtual_attr1, :virtual_attr2]
Or you can use the as_json serializer directly:
my_object.as_json(methods: [:virtual_attr1, :virtual_attr2])
The return you see in the console is nothing else but the value of to_s. For this case, code should be better than natural language, take a look in the following code and see if you understand
class A
end
=> nil
A.new
=> #<A:0xb73d1528>
A.new.to_s
=> "#<A:0xb73d1528>"
class A
def to_s
"foobar"
end
end
=> nil
A.new
=> ble
A.new.to_s
=> "ble"
You can see this output because ActiveRecord::Base defines a method to_s that take into account only the attributes that are defined in the database, not the attr_accessor methods, maybe using the attributes call.
This is not exactly a question, it's rather a report on how I solved an issue with write_attribute when the attribute is an object, on Rails' Active Record. I hope this can be useful to others facing the same problem.
Let me explain with an example. Suppose you have two classes, Book and Author:
class Book < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :books
end
Very simple. But, for whatever reason, you need to override the author= method on Book. As I'm new to Rails, I've followed the Sam Ruby's suggestion on Agile Web Development with Rails: use attribute_writer private method. So, my first try was:
class Book < ActiveRecord::Base
belongs_to :author
def author=(author)
author = Author.find_or_initialize_by_name(author) if author.is_a? String
self.write_attribute(:author, author)
end
end
Unfortunately, this does not work. That's what I get from console:
>> book = Book.new(:name => "Alice's Adventures in Wonderland", :pub_year => 1865)
=> #<Book id: nil, name: "Alice's Adventures in Wonderland", pub_year: 1865, author_id: nil, created_at: nil, updated_at: nil>
>> book.author = "Lewis Carroll"
=> "Lewis Carroll"
>> book
=> #<Book id: nil, name: "Alice's Adventures in Wonderland", pub_year: 1865, author_id: nil, created_at: nil, updated_at: nil>
>> book.author
=> nil
It seems that Rails does not recognize it is an object and makes nothing: after the attribuition, author is still nil! Of course, I could try write_attribute(:author_id, author.id), but it does not help when the author is not saved yet (it still has no id!) and I need the objects be saved together (author must be saved only if book is valid).
After search a lot for a solution (and try many other things in vain), I found this message: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/4fe057494c6e23e8, so finally I could had some working code:
class Book < ActiveRecord::Base
belongs_to :author
def author_with_lookup=(author)
author = Author.find_or_initialize_by_name(author) if author.is_a? String
self.author_without_lookup = author
end
alias_method_chain :author=, :lookup
end
This time, the console was nice to me:
>> book = Book.new(:name => "Alice's Adventures in Wonderland", :pub_year => 1865)
=> #<Book id: nil, name: "Alice's Adventures in Wonderland", pub_year: 1865, author_id: nil, created_at: nil, updated_at: nil>
>> book.author = "Lewis Carroll"=> "Lewis Carroll"
>> book
=> #<Book id: nil, name: "Alice's Adventures in Wonderland", pub_year: 1865, author_id: nil, created_at: nil, updated_at: nil>
>> book.author
=> #<Author id: nil, name: "Lewis Carroll", created_at: nil, updated_at: nil>
The trick here is the alias_method_chain, that creates an interceptor (in this case author_with_lookup) and an alternative name to the old setter (author_without_lookup). I confess it took some time to understand this arrangement and I'd be glad if someone care to explain it in detail, but what surprised me was the lack of information about this kind of problem. I have to google a lot to find just one post, that by the title seemed initially unrelated to the problem. I'm new to Rails, so what do you think guys: is this a bad practice?
I recommend creating a virtual attribute instead of overriding the author= method.
class Book < ActiveRecord::Base
belongs_to :author
def author_name=(author_name)
self.author = Author.find_or_initialize_by_name(author_name)
end
def author_name
author.name if author
end
end
Then you could do cool things like apply it to a form field.
<%= f.text_field :author_name %>
Would this work for your situation?
When you override the accessor, you have to set an actual DB attribute for write_attribute and self[:the_attribute]=, and not the name of the association-generated attribute you're overriding. This works for me.
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
ActiveRecord::Schema.define do
create_table(:books) {|t| t.string :title }
create_table(:authors) {|t| t.string :name }
end
class Book < ActiveRecord::Base
belongs_to :author
def author=(author_name)
found_author = Author.find_by_name(author_name)
if found_author
self[:author_id] = found_author.id
else
build_author(:name => author_name)
end
end
end
class Author < ActiveRecord::Base
end
Author.create!(:name => "John Doe")
Author.create!(:name => "Tolkien")
b1 = Book.new(:author => "John Doe")
p b1.author
# => #<Author id: 1, name: "John Doe">
b2 = Book.new(:author => "Noone")
p b2.author
# => #<Author id: nil, name: "Noone">
b2.save
p b2.author
# => #<Author id: 3, name: "Noone">
I strongly recommend doing what Ryan Bates suggests, though; create a new author_name attribute and leave the association generated methods as they are. Less fuzz, less confusion.
I solved this problem using alias_method
class Book < ActiveRecord::Base
belongs_to :author
alias_method :set_author, :author=
def author=(author)
author = Author.find_or_initialize_by_name(author) if author.is_a? String
set_author(author)
end
end