ES
2010-Apr-01 13:00 UTC
Assigning values of an object when it is created based on mysql queries
I want to use a value entered in by a user in an object''s form to look up another value in a reference table and assign that value to the corresponding attribute in the object. How do I do that? Right now I have in my controller create method: if @number = Work.find_by_sql( "SELECT inv FROM samples_development.works WHERE worknbr REGEXP ''" + @sample[''work_number''] + "'';" ) @sample.inv = @number end But it assigns something odd to the number attribute that looks like this: !ruby/object:Work attributes: inv: INV 1986/MR 690/2201 attributes_cache: {} - Should I be doing this in the model instead? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Apr-01 13:07 UTC
Re: Assigning values of an object when it is created based on mysql queries
On Apr 1, 2:00 pm, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to use a value entered in by a user in an object''s form to look > up another value in a reference table and assign that value to the > corresponding attribute in the object. How do I do that? > > Right now I have in my controller create method: > > if @number = Work.find_by_sql( "SELECT inv FROM > samples_development.works WHERE worknbr REGEXP ''" + > @sample[''work_number''] + "'';" )find_by_sql always returns an array of instances of the corresponding class (Work in this case) no matter what the select clause is. The columns in the result set are all there as attributes though Fred> @sample.inv = @number > end > > But it assigns something odd to the number attribute that looks like > this: > > !ruby/object:Work attributes: inv: INV 1986/MR 690/2201 > attributes_cache: {} - > > Should I be doing this in the model instead?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ES
2010-Apr-01 13:30 UTC
Re: Assigning values of an object when it is created based on mysql queries
Ok, thanks! So would this be the way to do it? @i = Work.find_by_sql( "SELECT inv FROM samples_development.works WHERE worknbr REGEXP ''" + @sample[''work_number''] + "'';" ) @sample.inv = @i[''inv''] I keep getting "can''t convert String into Integer" On Apr 1, 3:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 1, 2:00 pm, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I want to use a value entered in by a user in an object''s form to look > > up another value in a reference table and assign that value to the > > corresponding attribute in the object. How do I do that? > > > Right now I have in my controller create method: > > > if @number = Work.find_by_sql( "SELECT inv FROM > > samples_development.works WHERE worknbr REGEXP ''" + > > @sample[''work_number''] + "'';" ) > > find_by_sql always returns an array of instances of the corresponding > class (Work in this case) no matter what the select clause is. The > columns in the result set are all there as attributes though > > Fred > > > @sample.inv = @number > > end > > > But it assigns something odd to the number attribute that looks like > > this: > > > !ruby/object:Work attributes: inv: INV 1986/MR 690/2201 > > attributes_cache: {} - > > > Should I be doing this in the model instead? > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ES
2010-Apr-01 13:33 UTC
Re: Assigning values of an object when it is created based on mysql queries
No, that was wrong, it returns an array, so I had to add .first : @i = Work.find_by_sql( "SELECT inv FROM samples_development.works WHERE worknbr REGEXP ''" + @sample[''work_number''] + "'';" ) @sample.inv = @i.first[''inv''] On Apr 1, 3:30 pm, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok, thanks! So would this be the way to do it? > > @i = Work.find_by_sql( "SELECT inv FROM samples_development.works > WHERE worknbr REGEXP ''" + @sample[''work_number''] + "'';" ) > @sample.inv = @i[''inv''] > > I keep getting "can''t convert String into Integer" > > On Apr 1, 3:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Apr 1, 2:00 pm, ES <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I want to use a value entered in by a user in an object''s form to look > > > up another value in a reference table and assign that value to the > > > corresponding attribute in the object. How do I do that? > > > > Right now I have in my controller create method: > > > > if @number = Work.find_by_sql( "SELECT inv FROM > > > samples_development.works WHERE worknbr REGEXP ''" + > > > @sample[''work_number''] + "'';" ) > > > find_by_sql always returns an array of instances of the corresponding > > class (Work in this case) no matter what the select clause is. The > > columns in the result set are all there as attributes though > > > Fred > > > > @sample.inv = @number > > > end > > > > But it assigns something odd to the number attribute that looks like > > > this: > > > > !ruby/object:Work attributes: inv: INV 1986/MR 690/2201 > > > attributes_cache: {} - > > > > Should I be doing this in the model instead? > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.