Hi everyone,
I'm having a weird issue
I have a Study model that has several fields, but to mention a few (or
that ones that I'm having troubles with) are
- profesion_related
- profesion_name
I have this in my controller
def create
@study = Study.new(params[:study])
respond_to do |format|
if @study.save
#code here
else
#more code here
end
end
end
and in my model
class Study < AR....
#there are other attr_accessible but just wanted to show this
attr_accessible :profesion_related
attr_accessor :profesion_related
def profesion_related=(id)
profesion_parent = Profesion.find(id)
######below here the last attribute is the one that is empty
new_profesion = Profesion.create({m_code: profesion_parent.m_code,
g_code: profesion_parent.g_code, name: self.profesion_name})
end
end
I'm getting an error there because self.profesion_name is empty, but
in my log I see that it's being sent and also if I put in my create
action
@study = Study.new(params[:study]
-> puts @study.to_yaml
respond_to
I can see that the object @study has every single attribute filled,
but inside the profesion_related method in my model, the
self.profesion_name is empty
What could be wrong with this?
Hope someone can help me
Thanks in advance
on 2013-01-02 23:44
on 2013-01-03 04:33
It's possible that the profesion_name is not set yet -- you might want to consider creating the Profesion model in a before_save callback. I suspect there are some issues with how this is model, but hopeful this suggestion will get you moving foward. Cheers, Nicholas
on 2013-01-03 09:23
On Jan 2, 10:42pm, JavierQQ <jquari...@gmail.com> wrote: > Hi everyone, > I can see that the object @study has every single attribute filled, > but inside the profesion_related method in my model, the > self.profesion_name is empty > > What could be wrong with this? > Hope someone can help An activerecord object's initialize method basically does attributes.each do |attr_name, value | send("#{attr_name}=", value) end So at some point it will call self.profession_name = and at some point it will call self.profession_related but there's no particular order that is is guaranteed to happen in (on ruby 1.9 I wouldn't be surprised if it was the order of the fields on the page) You might want to consider building the profession either outside of new entirely or in a callback. It's also possible that how you've modelled it is making it more complicated than it has to be but it's hard to tell that from here. Fred
on 2013-01-03 14:31
Hi guys, Thanks both for the reply, I know it could be done by using a callback but there are more code inside that method. I'm using profesion_related as mentioned here http://railscasts.com/episodes/16-virtual-attributes (as well as other code inside that method) On Thu, Jan 3, 2013 at 3:22 AM, Frederick Cheung <frederick.cheung@gmail.com > wrote: > surprised if it was the order of the fields on the page) > > Indeed, I was trying so many times that I was wondering if it may have an special order of setting the values and it may be doing that method BEFORE filling the other values. It seems that the best way to avoid this is by doing a callback (in that case I will have to change more code :/). Is a way of telling AR to use that method after every other? Thanks Javier
on 2013-01-03 18:03
On Thu, Jan 3, 2013 at 8:52 AM, Dheeraj Kumar <a.dheeraj.kumar@gmail.com>wrote: > No, there isn't. Callbacks are your best bet. > > In the end I made a before_validation callback and inside that I'm using the virtual attribute, but I'm still wondering how AR order the list of attributes I started here https://github.com/rails/rails/blob/3-2-stable/act... but I'm not sure where should I go next. Hope somebody can guide me Thanks Javier
on 2013-01-03 20:18
IMHO AR lists the attributes as it receives them from schema and any virtual attributes from the model. This is because attributes is a hash, and Ruby 1.9 maintains the key order in a hash. If your code depends on the order of attributes defined, it's definitely a smell. -- Dheeraj Kumar
on 2013-01-03 21:23
On Thu, Jan 3, 2013 at 12:02 PM, Javier Quarite <jquarites@gmail.com> wrote: > virtual attribute, but I'm still wondering how AR order the list of > attributes > > I started here > https://github.com/rails/rails/blob/3-2-stable/act... > but I'm not sure where should I go next. Hope somebody can guide me Here is a link to view the assignment of attributes in ActiveRecord code base: https://github.com/rails/rails/blob/3-2-stable/act...
on 2013-01-04 22:10
Well, I did a before_validation callback def bv_callback puts self.to_yaml end and I can see that the object has profesion_name (and every other) attribute setted but here def profesion_related=(id) puts self.to_yaml end there are only 3 attributes filled. I'm still not getting how does AR manages to fill each attribute of the object after being saved
on 2013-01-04 22:20
Simple. First, all setter methods are called on AR, and I'm guessing profesion_related= was called the fourth, so only three attributes are filled. After all setter methods are called (which means all attributes are populated) the callbacks start, the first being the before_validation one, which shows you the entire AR object. -- Dheeraj Kumar
on 2013-01-05 00:09
On Fri, Jan 4, 2013 at 4:19 PM, Dheeraj Kumar <a.dheeraj.kumar@gmail.com>wrote: > Simple. First, all setter methods are called on AR, and I'm guessing > profesion_related= was called the fourth, so only three attributes are > filled. After all setter methods are called (which means all attributes are > populated) the callbacks start, the first being the before_validation one, > which shows you the entire AR object. > > If that's the case... I'm not sure how to proceed then :( I thought active record would set object attributes first, and then virtual attributes or attr_writer methods I'm doing this writer method because of this def profesion_related=(value) if value.to_i.to_s == value #some code here if value is a number else #create a new profesion if is an string end #there's a select input that changes the type of the profesion_related input in my form (between a select input and a text input end As far as I tried I can't retrieve profesion_related value in my before_validation callback cause its a virtual attribute
on 2013-01-05 05:07
You should be able to retrieve the value in the callback. If it isn't, you're doing something wrong. Make sure you remove your setters. Use just the callback. If it doesn't work, gist your code. -- Dheeraj Kumar
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.