Hi all, Coming from the Java & Hibernate world, I was confused when trying out this piece of line of code: class Task < ActiveRecord::Base def status=(status) status = ''P'' write_attribute(:status, status) end end I suspect that the value of :status in the database would be P, because I presume rails would set the attribute value first before saving it to the database. But instead I am getting this error: ActiveRecord::StatementInvalid: Mysql::Error: Column ''status'' cannot be null So what does overwriting default accessors used for in real life apps? And how do we set the value before it is saved to the database in Rails? Thank you very much in advance for the insights. -- Certified Scrum Master http://twitter.com/scrum8 | http://blog.scrum8.com | http://jobs.scrum8.com
Joshua Partogi wrote:> Hi all, > > Coming from the Java & Hibernate world, I was confused when trying out > this piece of line of code: > class Task < ActiveRecord::Base > def status=(status) > status = ''P'' > write_attribute(:status, status) > end > endThis won''t quite work. But before I try to improve it...what are you trying to do? Set status to P no matter the argument that the function is called with? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Oct 12, 10:31 am, Marnen Laibow-Koser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Joshua Partogi wrote: > > Hi all, > > > Coming from the Java & Hibernate world, I was confused when trying out > > this piece of line of code: > > class Task < ActiveRecord::Base > > def status=(status) > > status = ''P'' > > write_attribute(:status, status) > > end > > end > > This won''t quite work. But before I try to improve it...what are you > trying to do? Set status to P no matter the argument that the function > is called with? >Well there are more logic there actually to set the status. I was hoping that this accessors would be able to set or change the value of :status. But the behaviours doesn''t seems to be like that. So what is this accessors used for? Is it only for the view level? Regards, -- Certified Scrum Master http://blog.scrum8.com | http://jobs.scrum8.com | http://twitter.com/scrum8
Joshua Partogi wrote:> On Oct 12, 10:31 am, Marnen Laibow-Koser > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > end >> >> This won''t quite work. But before I try to improve it...what are you >> trying to do? Set status to P no matter the argument that the function >> is called with? >> > > Well there are more logic there actually to set the status. I was > hoping that this accessors would be able to set or change the value of > :status. But the behaviours doesn''t seems to be like that.It can be, but you''ll have to do it slightly differently. I''ll help you, but you''re going to have to explain exactly what you want to have happen.> So what is > this accessors used for? Is it only for the view level?No. It''s just that your syntax wasn''t quite correct.> > Regards, > > > -- > Certified Scrum Master > http://blog.scrum8.com | http://jobs.scrum8.com | > http://twitter.com/scrum8Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Hi Marnen On Oct 12, 3:04 pm, Marnen Laibow-Koser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Joshua Partogi wrote: > > On Oct 12, 10:31 am, Marnen Laibow-Koser > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > end > > >> This won''t quite work. But before I try to improve it...what are you > >> trying to do? Set status to P no matter the argument that the function > >> is called with? > > > Well there are more logic there actually to set the status. I was > > hoping that this accessors would be able to set or change the value of > > :status. But the behaviours doesn''t seems to be like that. > > It can be, but you''ll have to do it slightly differently. I''ll help > you, but you''re going to have to explain exactly what you want to have > happen.The point is I want to set the value of the status before it is saved to the database.> > So what is > > this accessors used for? Is it only for the view level? > > No. It''s just that your syntax wasn''t quite correct.Many thanks. -- Certified Scrum Master http://blog.scrum8.com | http://jobs.scrum8.com | http://twitter.com/scrum8
Joshua Partogi wrote:> Hi Marnen > > > On Oct 12, 3:04 pm, Marnen Laibow-Koser > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > hoping that this accessors would be able to set or change the value of >> > :status. But the behaviours doesn''t seems to be like that. >> >> It can be, but you''ll have to do it slightly differently. I''ll help >> you, but you''re going to have to explain exactly what you want to have >> happen. > > The point is I want to set the value of the status before it is saved > to the database.Then you want something like def status=(value) new_value = do_something_to(value) self[:status] = new_value # assuming this class is derived from AR end The reason your attempt to do status = value didn''t work is that it was just calling the same method. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Oct 13, 2:24 am, Marnen Laibow-Koser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Then you want something like > > def status=(value) > new_value = do_something_to(value) > self[:status] = new_value # assuming this class is derived from AR > end > > The reason your attempt to do status = value didn''t work is that it was > just calling the same method.Aaaah. Or course! You can omit the parantheses in ruby method call. Thanks heaps for pointing me this out. Kind regards, -- Certified Scrum Master http://blog.scrum8.com | http://jobs.scrum8.com | http://twitter.com/scrum8