Hi, I''ve got a database table called "resume" which contains id int user_id int name varchar value text ie. a collection of name/value pairs grouped by user_id. What I''d like to do is something like this: def index myResume = Resume.find(SOMEQUERY on user_id) value = myResume.name end I imagine I need something in my resume model to do this, but I have no idea how to phrase an accurate search of Google or the docs. Does anyone have an idea on how to do this, or how to achieve an efficient alternative? Regards, Colin
On 5/4/05, Colin Ramsay <colinramsay-BFpX1XrDWuBdtzWjJqcUOrVCufUGDwFn@public.gmane.org> wrote:> Hi, > > I''ve got a database table called "resume" which contains > > id int > user_id int > name varchar > value text > > ie. a collection of name/value pairs grouped by user_id. > > What I''d like to do is something like this: > > def index > myResume = Resume.find(SOMEQUERY on user_id) > value = myResume.name > end > > I imagine I need something in my resume model to do this, but I have no > idea how to phrase an accurate search of Google or the docs. Does anyone > have an idea on how to do this, or how to achieve an efficient alternative? > > Regards, > Colin > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Probably going to look something like this, assuming you have a user model stored in the session. Resume.find(:first, :conditions => [ "user_id = ?", @session[:user][:id] ]) A good place to look would be the docs for ActiveRecord::Base#find method [1] Jason [1] http://ar.rubyonrails.com
Jason Foreman wrote> Probably going to look something like this, assuming you have a user > >model stored in the session. > >Resume.find(:first, :conditions => [ "user_id = ?", @session[:user][:id] ]) > >A good place to look would be the docs for ActiveRecord::Base#find method [1] > > >Jason >I should have been more explicit - I know to use the find method, it''s really the second part of my post that it''s the issue. I could do it like this: value = some_helper(user_id,name) ... def some_helper myRecord = Resume.find(:conditions => ["user_id = " + user_id.to_s " AND name = " + name]) return myRecord.value end But this doesn''t seem like a very nice way for doing it.
Put it in your model: class Resume < ActiveRecord::Base def self.find_by_user(user_id, name) find(:first, :conditions => [''user_id = ? and name = ?'', user_id, name]).value end end On 5/4/05, Colin Ramsay <colinramsay-BFpX1XrDWuBdtzWjJqcUOrVCufUGDwFn@public.gmane.org> wrote:> Jason Foreman wrote > > > Probably going to look something like this, assuming you have a user > > > >model stored in the session. > > > >Resume.find(:first, :conditions => [ "user_id = ?", @session[:user][:id] ]) > > > >A good place to look would be the docs for ActiveRecord::Base#find method [1] > > > > > >Jason > > > I should have been more explicit - I know to use the find method, it''s > really the second part of my post that it''s the issue. I could do it > like this: > > value = some_helper(user_id,name) > ... > def some_helper > myRecord = Resume.find(:conditions => ["user_id = " + user_id.to_s " > AND name = " + name]) > return myRecord.value > end > > But this doesn''t seem like a very nice way for doing it. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- rick http://techno-weenie.net
Colin Ramsay wrote:> I should have been more explicit - I know to use the find method, it''s > really the second part of my post that it''s the issue. I could do it > like this: > > value = some_helper(user_id,name) > ... > def some_helper > myRecord = Resume.find(:conditions => ["user_id = " + user_id.to_s > " AND name = " + name]) > return myRecord.value > end > > But this doesn''t seem like a very nice way for doing it.Replying to myself here, but using the above method means I can''t do something like this: def index @resume = Resume.find(:first, :conditions => [ "user_id = ?", user_d) end index.rhtml: <%= text_field "resume","value" %> I suppose what I want is some kind of virtual accessor on the model...?
Rick Olson wrote:>Put it in your model: > >class Resume < ActiveRecord::Base > def self.find_by_user(user_id, name) > find(:first, :conditions => [''user_id = ? and name = ?'', user_id, >name]).value > end >end > >Hmm that''s quite useful, but I don''t think I could use it with the formhelpers, e.g.: <%= text_field "resume","value" %> Thanks for the comments so far.
Colin, On 4.5.2005, at 17:12, Colin Ramsay wrote:>> > Hmm that''s quite useful, but I don''t think I could use it with the > formhelpers, e.g.: > > <%= text_field "resume","value" %>I don''t quite follow you here. If you have an object named @resume which has a method "value" you _can_ use text_field. There is no special intelligence in text_field (or any other form helpers) so it doesn''t matter how @resume was created. All that matters is that it responds_to?("value"). //jarkko> > Thanks for the comments so far. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine wrote:> I don''t quite follow you here. If you have an object named @resume > which has a method "value" you _can_ use text_field. There is no > special intelligence in text_field (or any other form helpers) so it > doesn''t matter how @resume was created. All that matters is that it > responds_to?("value"). > > //jarkkoThere is a value method. But following this example: @resume = Resume.find(:all, :conditions => [ "user_id = ?", user_id) In the above example, @resume would be an array of all records matching that user_id. There would be lots of records with lots of different ''values''. Here''s an example of the returned values in CSV format: id,user_id,name,value 1,5,age,376 2,5,sex,female 3,5,location,england So I''d want to do <%= text_field "resume","age" %> and have it do a lookup on the "name" column for the value of "age". What is responds_to?("value")?
> id,user_id,name,value > 1,5,age,376 > 2,5,sex,female > 3,5,location,england >I hope you have a *GOOD* reason for modelling your data this way, because you''ve just made life miserable :) Done this way, there is no simple way to get at the ''attributes'' of a particular instance of a ''Resume'', because there are multiple rows representing one ''object''. You''re gonna have to do some fancy meta work to get those to be attributes of your model. Look for a thread by Adelle Hartley about doing dynamic relations, that might point you in the right direction. May the Schwartz be with you! Jason
Jason Foreman wrote:>I hope you have a *GOOD* reason for modelling your data this way, >because you''ve just made life miserable :) > >Done this way, there is no simple way to get at the ''attributes'' of a >particular instance of a ''Resume'', because there are multiple rows >representing one ''object''. You''re gonna have to do some fancy meta >work to get those to be attributes of your model. > >Look for a thread by Adelle Hartley about doing dynamic relations, >that might point you in the right direction. May the Schwartz be with >you! > >It''s possible my reasons aren''t good enough, it''s just that if I were to have a column for each of the possible "names" I''d have a *lot* of columns. It''s beginning to seem like that might be the smarter (read: "quicker") option. Thanks for the help :)
On 5/5/05, Colin Ramsay <colinramsay-BFpX1XrDWuBdtzWjJqcUOrVCufUGDwFn@public.gmane.org> wrote:> Jason Foreman wrote: > > >I hope you have a *GOOD* reason for modelling your data this way, > >because you''ve just made life miserable :) > > > >Done this way, there is no simple way to get at the ''attributes'' of a > >particular instance of a ''Resume'', because there are multiple rows > >representing one ''object''. You''re gonna have to do some fancy meta > >work to get those to be attributes of your model. > > > >Look for a thread by Adelle Hartley about doing dynamic relations, > >that might point you in the right direction. May the Schwartz be with > >you! > > > > > It''s possible my reasons aren''t good enough, it''s just that if I were to > have a column for each of the possible "names" I''d have a *lot* of > columns. It''s beginning to seem like that might be the smarter (read: > "quicker") option. Thanks for the help :)It''s possible that this is a quicker way to implement it. But splitting your models up like that isn''t necessarily a bad thing. It allows you to support more, or fewer, items in the future. Just one nitpick, I''d call those things ResumeDetail and have them belong_to a Resume. However, if you''re never going to need to add more fields or remove them, or have someone with multiple values for one of those fields, then go with the ''wide table'' option with lots and lots of fields.> _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
Michael Koziarski wrote:>On 5/5/05, Colin Ramsay <colinramsay-BFpX1XrDWuBdtzWjJqcUOrVCufUGDwFn@public.gmane.org> wrote: > > >>Jason Foreman wrote: >> >> >> >>>I hope you have a *GOOD* reason for modelling your data this way, >>>because you''ve just made life miserable :) >>> >>>Done this way, there is no simple way to get at the ''attributes'' of a >>>particular instance of a ''Resume'', because there are multiple rows >>>representing one ''object''. You''re gonna have to do some fancy meta >>>work to get those to be attributes of your model. >>> >>>Look for a thread by Adelle Hartley about doing dynamic relations, >>>that might point you in the right direction. May the Schwartz be with >>>you! >>> >>> >>> >>> >>It''s possible my reasons aren''t good enough, it''s just that if I were to >>have a column for each of the possible "names" I''d have a *lot* of >>columns. It''s beginning to seem like that might be the smarter (read: >>"quicker") option. Thanks for the help :) >> >> > >It''s possible that this is a quicker way to implement it. But >splitting your models up like that isn''t necessarily a bad thing. It >allows you to support more, or fewer, items in the future. > >Just one nitpick, I''d call those things ResumeDetail and have them >belong_to a Resume. > >However, if you''re never going to need to add more fields or remove >them, or have someone with multiple values for one of those fields, >then go with the ''wide table'' option with lots and lots of fields. >What I''ve ended up doing is to create an application helper similar to text_field which does a lookup on the name field. Not efficient in database terms, but it''s better than having a huge table with lots of columns for our purposes. I think caching could improve the performance - I''ll post the results of my dabbling tomorrow.