I''m just getting into RoR, coming from a long PHP background. So far I LOVE IT! I''m hitting a roadblock with updating my mysql database. For simplicities sake, I have 2 tables and here are the relevant columns. Table Users id name email Table Issues id createdby assignedto reportedby In my models I have everything mapped properly I believe. When I try to update the Issues table @issue = Issue.find(:id) @issue.update_attributes(params[:issue]) I get an error, "User expected, got String" So my question is, should I be updating the table a different way? Is there another method I should be using? Thanks for any help you could give a beginner! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> @issue = Issue.find(:id) > @issue.update_attributes(params[:issue]) >It would be (assuming, that there is a column issue) @issue.update_attributes(:issue => params[:issue]) or @issue.update_attribute(:issue, params[:issue]) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m just getting into RoR, coming from a long PHP background. So far I > LOVE IT! > > I''m hitting a roadblock with updating my mysql database. For > simplicities sake, I have 2 tables and here are the relevant columns. > > Table Users > id > name > email > > Table Issues > id > createdby > assignedto > reportedby > > In my models I have everything mapped properly I believe. > > When I try to update the Issues table > > @issue = Issue.find(:id) > @issue.update_attributes(params[:issue]) > > I get an error, "User expected, got String"What''s in params[:issue] ? From the error message i''d guess that issue belongs_to :user, and that params[:issue] contains a value for :user. This won''t work (as the error message says from the form you''ll get a string whereas user= needs an instance of User. The easiest way is usually for the form to submit user_id Fred> > So my question is, should I be updating the table a different way? Is > there another method I should be using? Thanks for any help you could > give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
params[:issue] contains the form elements from the form that is being posted. Example is the dropdown that is causing the issues. It''s code is as follows: <select name="issue[assignedto]" id="issue_assignedto"> <%@users.each do |users|%> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) %>selected="selected"<%end%>><%=users.name%></option> <%end%> </select></td></tr> @users is constructed as follows in the controller: @users = User.find(:all, :order => "name") There is no column in the table issues.. Any thoughts? On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m just getting into RoR, coming from a long PHP background. So far I > > LOVE IT! > > > I''m hitting a roadblock with updating my mysql database. For > > simplicities sake, I have 2 tables and here are the relevant columns. > > > Table Users > > id > > name > > email > > > Table Issues > > id > > createdby > > assignedto > > reportedby > > > In my models I have everything mapped properly I believe. > > > When I try to update the Issues table > > > @issue = Issue.find(:id) > > @issue.update_attributes(params[:issue]) > > > I get an error, "User expected, got String" > > What''s in params[:issue] ? From the error message i''d guess that issue > belongs_to :user, and that params[:issue] contains a value for :user. > This won''t work (as the error message says from the form you''ll get a > string whereas user= needs an instance of User. > The easiest way is usually for the form to submit user_id > > Fred > > > > > So my question is, should I be updating the table a different way? Is > > there another method I should be using? Thanks for any help you could > > give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Here are my 2 models class Issue < ActiveRecord::Base belongs_to :priority belongs_to :problemdetail belongs_to :createdby, :class_name => "User", :foreign_key => "createdby" belongs_to :reportedby, :class_name => "User", :foreign_key => "reportedby" belongs_to :assignedto, :class_name => "User", :foreign_key => "assignedto" belongs_to :site belongs_to :carrier; end class User < ActiveRecord::Base has_many :messages has_many :issues, :class_name => "Issue", :foreign_key => "createdby" has_many :issues, :class_name => "Issue", :foreign_key => "assignedto" has_many :issues, :class_name => "Issue", :foreign_key => "reportedby" end On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> params[:issue] contains the form elements from the form that is being > posted. Example is the dropdown that is causing the issues. It''s code > is as follows: > > <select name="issue[assignedto]" id="issue_assignedto"> > <%...@users.each do |users|%> > <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) > %>selected="selected"<%end%>><%=users.name%></option> > <%end%> > </select></td></tr> > > @users is constructed as follows in the controller: > > @users = User.find(:all, :order => "name") > > There is no column in the table issues.. Any thoughts? > > On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''m just getting into RoR, coming from a long PHP background. So far I > > > LOVE IT! > > > > I''m hitting a roadblock with updating my mysql database. For > > > simplicities sake, I have 2 tables and here are the relevant columns. > > > > Table Users > > > id > > > name > > > email > > > > Table Issues > > > id > > > createdby > > > assignedto > > > reportedby > > > > In my models I have everything mapped properly I believe. > > > > When I try to update the Issues table > > > > @issue = Issue.find(:id) > > > @issue.update_attributes(params[:issue]) > > > > I get an error, "User expected, got String" > > > What''s in params[:issue] ? From the error message i''d guess that issue > > belongs_to :user, and that params[:issue] contains a value for :user. > > This won''t work (as the error message says from the form you''ll get a > > string whereas user= needs an instance of User. > > The easiest way is usually for the form to submit user_id > > > Fred > > > > So my question is, should I be updating the table a different way? Is > > > there another method I should be using? Thanks for any help you could > > > give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On 15 Jul 2008, at 11:13, Rob Tucker wrote:> > Here are my 2 models > > class Issue < ActiveRecord::Base > belongs_to :priority > belongs_to :problemdetail > belongs_to :createdby, :class_name => "User", :foreign_key => > "createdby" > belongs_to :reportedby, :class_name => "User", :foreign_key => > "reportedby" > belongs_to :assignedto, :class_name => "User", :foreign_key => > "assignedto" > belongs_to :site > belongs_to :carrier; >This is probably the problem :you have associations with the same name as attrribute. You want to set the createdby column to 123456 but rails things you''re trying to change the createdby association to 123456. If you follow the rails_conventions the foreign_keys are name foo_id and the associations are named foo and the problem should go away.> end > > class User < ActiveRecord::Base > has_many :messages > has_many :issues, :class_name => "Issue", :foreign_key => > "createdby" > has_many :issues, :class_name => "Issue", :foreign_key => > "assignedto" > has_many :issues, :class_name => "Issue", :foreign_key => > "reportedby"Won''t be the problem here, but you can''t do that: the has_manys have to have distinct names (ie has_many :created_issues; has_many :reported_issues etc...(> > > end > > > On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> params[:issue] contains the form elements from the form that is being >> posted. Example is the dropdown that is causing the issues. It''s code >> is as follows: >> >> <select name="issue[assignedto]" id="issue_assignedto"> >> <%...@users.each do |users|%> >> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) >> %>selected="selected"<%end%>><%=users.name%></option> >> <%end%> >> </select></td></tr> >> >> @users is constructed as follows in the controller: >> >> @users = User.find(:all, :order => "name") >> >> There is no column in the table issues.. Any thoughts? >> >> On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >> >>> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>>> I''m just getting into RoR, coming from a long PHP background. So >>>> far I >>>> LOVE IT! >> >>>> I''m hitting a roadblock with updating my mysql database. For >>>> simplicities sake, I have 2 tables and here are the relevant >>>> columns. >> >>>> Table Users >>>> id >>>> name >>>> email >> >>>> Table Issues >>>> id >>>> createdby >>>> assignedto >>>> reportedby >> >>>> In my models I have everything mapped properly I believe. >> >>>> When I try to update the Issues table >> >>>> @issue = Issue.find(:id) >>>> @issue.update_attributes(params[:issue]) >> >>>> I get an error, "User expected, got String" >> >>> What''s in params[:issue] ? From the error message i''d guess that >>> issue >>> belongs_to :user, and that params[:issue] contains a value >>> for :user. >>> This won''t work (as the error message says from the form you''ll >>> get a >>> string whereas user= needs an instance of User. >>> The easiest way is usually for the form to submit user_id >> >>> Fred >> >>>> So my question is, should I be updating the table a different >>>> way? Is >>>> there another method I should be using? Thanks for any help you >>>> could >>>> give a beginner! > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That makes sense. Would you mind outlining how my models would look, should I change my database scheme? On Jul 15, 6:37 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 15 Jul 2008, at 11:13, Rob Tucker wrote: > > > > > Here are my 2 models > > > class Issue < ActiveRecord::Base > > belongs_to :priority > > belongs_to :problemdetail > > belongs_to :createdby, :class_name => "User", :foreign_key => > > "createdby" > > belongs_to :reportedby, :class_name => "User", :foreign_key => > > "reportedby" > > belongs_to :assignedto, :class_name => "User", :foreign_key => > > "assignedto" > > belongs_to :site > > belongs_to :carrier; > > This is probably the problem :you have associations with the same name > as attrribute. You want to set the createdby column to 123456 but > rails things you''re trying to change the createdby association to > 123456. > If you follow the rails_conventions the foreign_keys are name foo_id > and the associations are named foo and the problem should go away. > > > end > > > class User < ActiveRecord::Base > > has_many :messages > > has_many :issues, :class_name => "Issue", :foreign_key => > > "createdby" > > has_many :issues, :class_name => "Issue", :foreign_key => > > "assignedto" > > has_many :issues, :class_name => "Issue", :foreign_key => > > "reportedby" > > Won''t be the problem here, but you can''t do that: the has_manys have > to have distinct names (ie has_many :created_issues; > has_many :reported_issues etc...( > > > > > end > > > On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> params[:issue] contains the form elements from the form that is being > >> posted. Example is the dropdown that is causing the issues. It''s code > >> is as follows: > > >> <select name="issue[assignedto]" id="issue_assignedto"> > >> <%...@users.each do |users|%> > >> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) > >> %>selected="selected"<%end%>><%=users.name%></option> > >> <%end%> > >> </select></td></tr> > > >> @users is constructed as follows in the controller: > > >> @users = User.find(:all, :order => "name") > > >> There is no column in the table issues.. Any thoughts? > > >> On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >> wrote: > > >>> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>> I''m just getting into RoR, coming from a long PHP background. So > >>>> far I > >>>> LOVE IT! > > >>>> I''m hitting a roadblock with updating my mysql database. For > >>>> simplicities sake, I have 2 tables and here are the relevant > >>>> columns. > > >>>> Table Users > >>>> id > >>>> name > >>>> email > > >>>> Table Issues > >>>> id > >>>> createdby > >>>> assignedto > >>>> reportedby > > >>>> In my models I have everything mapped properly I believe. > > >>>> When I try to update the Issues table > > >>>> @issue = Issue.find(:id) > >>>> @issue.update_attributes(params[:issue]) > > >>>> I get an error, "User expected, got String" > > >>> What''s in params[:issue] ? From the error message i''d guess that > >>> issue > >>> belongs_to :user, and that params[:issue] contains a value > >>> for :user. > >>> This won''t work (as the error message says from the form you''ll > >>> get a > >>> string whereas user= needs an instance of User. > >>> The easiest way is usually for the form to submit user_id > > >>> Fred > > >>>> So my question is, should I be updating the table a different > >>>> way? Is > >>>> there another method I should be using? Thanks for any help you > >>>> could > >>>> give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On 15 Jul 2008, at 12:46, Rob Tucker wrote:> > That makes sense. Would you mind outlining how my models would look, > should I change my database scheme?You''d change your schema so that the keys are created_by_id, reported_by_id etc... your model would have belongs_to :created_by, :class_name => "User" (the foreign_key option is inferred form the assocation name and defaults to adding _id) Fred> > > On Jul 15, 6:37 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 15 Jul 2008, at 11:13, Rob Tucker wrote: >> >> >> >>> Here are my 2 models >> >>> class Issue < ActiveRecord::Base >>> belongs_to :priority >>> belongs_to :problemdetail >>> belongs_to :createdby, :class_name => "User", :foreign_key => >>> "createdby" >>> belongs_to :reportedby, :class_name => "User", :foreign_key => >>> "reportedby" >>> belongs_to :assignedto, :class_name => "User", :foreign_key => >>> "assignedto" >>> belongs_to :site >>> belongs_to :carrier; >> >> This is probably the problem :you have associations with the same >> name >> as attrribute. You want to set the createdby column to 123456 but >> rails things you''re trying to change the createdby association to >> 123456. >> If you follow the rails_conventions the foreign_keys are name foo_id >> and the associations are named foo and the problem should go away. >> >>> end >> >>> class User < ActiveRecord::Base >>> has_many :messages >>> has_many :issues, :class_name => "Issue", :foreign_key => >>> "createdby" >>> has_many :issues, :class_name => "Issue", :foreign_key => >>> "assignedto" >>> has_many :issues, :class_name => "Issue", :foreign_key => >>> "reportedby" >> >> Won''t be the problem here, but you can''t do that: the has_manys have >> to have distinct names (ie has_many :created_issues; >> has_many :reported_issues etc...( >> >> >> >>> end >> >>> On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> params[:issue] contains the form elements from the form that is >>>> being >>>> posted. Example is the dropdown that is causing the issues. It''s >>>> code >>>> is as follows: >> >>>> <select name="issue[assignedto]" id="issue_assignedto"> >>>> <%...@users.each do |users|%> >>>> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) >>>> %>selected="selected"<%end%>><%=users.name%></option> >>>> <%end%> >>>> </select></td></tr> >> >>>> @users is constructed as follows in the controller: >> >>>> @users = User.find(:all, :order => "name") >> >>>> There is no column in the table issues.. Any thoughts? >> >>>> On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>>> wrote: >> >>>>> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>>>>> I''m just getting into RoR, coming from a long PHP background. So >>>>>> far I >>>>>> LOVE IT! >> >>>>>> I''m hitting a roadblock with updating my mysql database. For >>>>>> simplicities sake, I have 2 tables and here are the relevant >>>>>> columns. >> >>>>>> Table Users >>>>>> id >>>>>> name >>>>>> email >> >>>>>> Table Issues >>>>>> id >>>>>> createdby >>>>>> assignedto >>>>>> reportedby >> >>>>>> In my models I have everything mapped properly I believe. >> >>>>>> When I try to update the Issues table >> >>>>>> @issue = Issue.find(:id) >>>>>> @issue.update_attributes(params[:issue]) >> >>>>>> I get an error, "User expected, got String" >> >>>>> What''s in params[:issue] ? From the error message i''d guess that >>>>> issue >>>>> belongs_to :user, and that params[:issue] contains a value >>>>> for :user. >>>>> This won''t work (as the error message says from the form you''ll >>>>> get a >>>>> string whereas user= needs an instance of User. >>>>> The easiest way is usually for the form to submit user_id >> >>>>> Fred >> >>>>>> So my question is, should I be updating the table a different >>>>>> way? Is >>>>>> there another method I should be using? Thanks for any help you >>>>>> could >>>>>> give a beginner! > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I went ahead and changed my column names reportedby -> reported_by_id assignedto -> assigned_to_id createdby -> created_by_id Here are my models class Issue < ActiveRecord::Base belongs_to :priority belongs_to :problemdetail belongs_to :created_by, :class_name => "User" belongs_to :reported_by, :class_name => "User" belongs_to :assigned_to, :class_name => "User" belongs_to :site belongs_to :carrier end class User < ActiveRecord::Base has_many :messages has_many :created_by, :class_name => "Issue" has_many :assigned_to, :class_name => "Issue" has_many :reported_by, :class_name => "Issue" #has_many :messages #has_many :issues, :class_name => "Issue", :foreign_key => "createdby" #has_many :issues, :class_name => "Issue", :foreign_key => "assignedto" #has_many :issues, :class_name => "Issue", :foreign_key => "reportedby" end It doesn''t appear to be working correctly still.. any other thoughts? I''m not able to access objects as I would expect. issue.reported_by.name etc won''t work. On Jul 15, 8:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 15 Jul 2008, at 12:46, Rob Tucker wrote: > > > > > That makes sense. Would you mind outlining how my models would look, > > should I change my database scheme? > > You''d change your schema so that the keys are created_by_id, > reported_by_id etc... > your model would have > > belongs_to :created_by, :class_name => "User" > > (the foreign_key option is inferred form the assocation name and > defaults to adding _id) > > Fred > > > > > On Jul 15, 6:37 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> On 15 Jul 2008, at 11:13, Rob Tucker wrote: > > >>> Here are my 2 models > > >>> class Issue < ActiveRecord::Base > >>> belongs_to :priority > >>> belongs_to :problemdetail > >>> belongs_to :createdby, :class_name => "User", :foreign_key => > >>> "createdby" > >>> belongs_to :reportedby, :class_name => "User", :foreign_key => > >>> "reportedby" > >>> belongs_to :assignedto, :class_name => "User", :foreign_key => > >>> "assignedto" > >>> belongs_to :site > >>> belongs_to :carrier; > > >> This is probably the problem :you have associations with the same > >> name > >> as attrribute. You want to set the createdby column to 123456 but > >> rails things you''re trying to change the createdby association to > >> 123456. > >> If you follow the rails_conventions the foreign_keys are name foo_id > >> and the associations are named foo and the problem should go away. > > >>> end > > >>> class User < ActiveRecord::Base > >>> has_many :messages > >>> has_many :issues, :class_name => "Issue", :foreign_key => > >>> "createdby" > >>> has_many :issues, :class_name => "Issue", :foreign_key => > >>> "assignedto" > >>> has_many :issues, :class_name => "Issue", :foreign_key => > >>> "reportedby" > > >> Won''t be the problem here, but you can''t do that: the has_manys have > >> to have distinct names (ie has_many :created_issues; > >> has_many :reported_issues etc...( > > >>> end > > >>> On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>>> params[:issue] contains the form elements from the form that is > >>>> being > >>>> posted. Example is the dropdown that is causing the issues. It''s > >>>> code > >>>> is as follows: > > >>>> <select name="issue[assignedto]" id="issue_assignedto"> > >>>> <%...@users.each do |users|%> > >>>> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) > >>>> %>selected="selected"<%end%>><%=users.name%></option> > >>>> <%end%> > >>>> </select></td></tr> > > >>>> @users is constructed as follows in the controller: > > >>>> @users = User.find(:all, :order => "name") > > >>>> There is no column in the table issues.. Any thoughts? > > >>>> On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >>>> wrote: > > >>>>> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>>>> I''m just getting into RoR, coming from a long PHP background. So > >>>>>> far I > >>>>>> LOVE IT! > > >>>>>> I''m hitting a roadblock with updating my mysql database. For > >>>>>> simplicities sake, I have 2 tables and here are the relevant > >>>>>> columns. > > >>>>>> Table Users > >>>>>> id > >>>>>> name > >>>>>> email > > >>>>>> Table Issues > >>>>>> id > >>>>>> createdby > >>>>>> assignedto > >>>>>> reportedby > > >>>>>> In my models I have everything mapped properly I believe. > > >>>>>> When I try to update the Issues table > > >>>>>> @issue = Issue.find(:id) > >>>>>> @issue.update_attributes(params[:issue]) > > >>>>>> I get an error, "User expected, got String" > > >>>>> What''s in params[:issue] ? From the error message i''d guess that > >>>>> issue > >>>>> belongs_to :user, and that params[:issue] contains a value > >>>>> for :user. > >>>>> This won''t work (as the error message says from the form you''ll > >>>>> get a > >>>>> string whereas user= needs an instance of User. > >>>>> The easiest way is usually for the form to submit user_id > > >>>>> Fred > > >>>>>> So my question is, should I be updating the table a different > >>>>>> way? Is > >>>>>> there another method I should be using? Thanks for any help you > >>>>>> could > >>>>>> give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
I figured it out! I was trying to set the name of the association to the value, not the name of the column in the table. I''m such a noob! Thanks for the help! On Jul 15, 8:36 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I went ahead and changed my column names > reportedby -> reported_by_id > assignedto -> assigned_to_id > createdby -> created_by_id > > Here are my models > > class Issue < ActiveRecord::Base > belongs_to :priority > belongs_to :problemdetail > belongs_to :created_by, :class_name => "User" > belongs_to :reported_by, :class_name => "User" > belongs_to :assigned_to, :class_name => "User" > belongs_to :site > belongs_to :carrier > > end > > class User < ActiveRecord::Base > has_many :messages > has_many :created_by, :class_name => "Issue" > has_many :assigned_to, :class_name => "Issue" > has_many :reported_by, :class_name => "Issue" > > #has_many :messages > #has_many :issues, :class_name => "Issue", :foreign_key => > "createdby" > #has_many :issues, :class_name => "Issue", :foreign_key => > "assignedto" > #has_many :issues, :class_name => "Issue", :foreign_key => > "reportedby" > > end > > It doesn''t appear to be working correctly still.. any other thoughts? > I''m not able to access objects as I would expect. > issue.reported_by.name etc won''t work. > > On Jul 15, 8:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 15 Jul 2008, at 12:46, Rob Tucker wrote: > > > > That makes sense. Would you mind outlining how my models would look, > > > should I change my database scheme? > > > You''d change your schema so that the keys are created_by_id, > > reported_by_id etc... > > your model would have > > > belongs_to :created_by, :class_name => "User" > > > (the foreign_key option is inferred form the assocation name and > > defaults to adding _id) > > > Fred > > > > On Jul 15, 6:37 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > >> On 15 Jul 2008, at 11:13, Rob Tucker wrote: > > > >>> Here are my 2 models > > > >>> class Issue < ActiveRecord::Base > > >>> belongs_to :priority > > >>> belongs_to :problemdetail > > >>> belongs_to :createdby, :class_name => "User", :foreign_key => > > >>> "createdby" > > >>> belongs_to :reportedby, :class_name => "User", :foreign_key => > > >>> "reportedby" > > >>> belongs_to :assignedto, :class_name => "User", :foreign_key => > > >>> "assignedto" > > >>> belongs_to :site > > >>> belongs_to :carrier; > > > >> This is probably the problem :you have associations with the same > > >> name > > >> as attrribute. You want to set the createdby column to 123456 but > > >> rails things you''re trying to change the createdby association to > > >> 123456. > > >> If you follow the rails_conventions the foreign_keys are name foo_id > > >> and the associations are named foo and the problem should go away. > > > >>> end > > > >>> class User < ActiveRecord::Base > > >>> has_many :messages > > >>> has_many :issues, :class_name => "Issue", :foreign_key => > > >>> "createdby" > > >>> has_many :issues, :class_name => "Issue", :foreign_key => > > >>> "assignedto" > > >>> has_many :issues, :class_name => "Issue", :foreign_key => > > >>> "reportedby" > > > >> Won''t be the problem here, but you can''t do that: the has_manys have > > >> to have distinct names (ie has_many :created_issues; > > >> has_many :reported_issues etc...( > > > >>> end > > > >>> On Jul 15, 5:35 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>>> params[:issue] contains the form elements from the form that is > > >>>> being > > >>>> posted. Example is the dropdown that is causing the issues. It''s > > >>>> code > > >>>> is as follows: > > > >>>> <select name="issue[assignedto]" id="issue_assignedto"> > > >>>> <%...@users.each do |users|%> > > >>>> <option value="<%=users.id%>"<%if(users.id == issue.assignedto.id) > > >>>> %>selected="selected"<%end%>><%=users.name%></option> > > >>>> <%end%> > > >>>> </select></td></tr> > > > >>>> @users is constructed as follows in the controller: > > > >>>> @users = User.find(:all, :order => "name") > > > >>>> There is no column in the table issues.. Any thoughts? > > > >>>> On Jul 15, 5:16 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > >>>> wrote: > > > >>>>> On Jul 15, 2:21 am, Rob Tucker <rtucke...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >>>>>> I''m just getting into RoR, coming from a long PHP background. So > > >>>>>> far I > > >>>>>> LOVE IT! > > > >>>>>> I''m hitting a roadblock with updating my mysql database. For > > >>>>>> simplicities sake, I have 2 tables and here are the relevant > > >>>>>> columns. > > > >>>>>> Table Users > > >>>>>> id > > >>>>>> name > > >>>>>> email > > > >>>>>> Table Issues > > >>>>>> id > > >>>>>> createdby > > >>>>>> assignedto > > >>>>>> reportedby > > > >>>>>> In my models I have everything mapped properly I believe. > > > >>>>>> When I try to update the Issues table > > > >>>>>> @issue = Issue.find(:id) > > >>>>>> @issue.update_attributes(params[:issue]) > > > >>>>>> I get an error, "User expected, got String" > > > >>>>> What''s in params[:issue] ? From the error message i''d guess that > > >>>>> issue > > >>>>> belongs_to :user, and that params[:issue] contains a value > > >>>>> for :user. > > >>>>> This won''t work (as the error message says from the form you''ll > > >>>>> get a > > >>>>> string whereas user= needs an instance of User. > > >>>>> The easiest way is usually for the form to submit user_id > > > >>>>> Fred > > > >>>>>> So my question is, should I be updating the table a different > > >>>>>> way? Is > > >>>>>> there another method I should be using? Thanks for any help you > > >>>>>> could > > >>>>>> give a beginner!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---