Beginner Question.

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!

@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])

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

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?

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

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...(

That makes sense. Would you mind outlining how my models would look, should I change my database scheme?

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

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.

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!