Forum: Ruby on Rails Changing primary.. Rails/mysql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cad24cf25cdf0bade2e508d30dd3cadd?d=identicon&s=25 Jay Mark (cypray)
on 2008-12-06 02:14
I am using Rails 2.0.2 with mysql database
Migrate sets the primary key to id.
Is it possible to change it to another column?
If yes, how?
Thanks for your help

Cypray.
247cd3d37084a3d6794076207bd9fbd7?d=identicon&s=25 Bobnation (Guest)
on 2008-12-06 06:22
(Received via mailing list)
First thing I would ask is "why?" What is the reason behind the
change?
Cad24cf25cdf0bade2e508d30dd3cadd?d=identicon&s=25 Jay Mark (cypray)
on 2008-12-06 07:50
Well, I am having trouble with this query:

class Author < ActiveRecord::Base

has_many :books

def author_age
    @results = Author.find :all, :conditions => ["age = ?",
params[:authors]]
     end
end



I have this on view\authors\show.html

<table border="1">
 <tr>
 <td width="20%"><p align="center"><i><b>Author Name</b></i></td>
 <td width="20%"><p align="center"><i><b>Age</b></i></td>
 </tr>


 <% @results.each do |result| %>

 <tr>

 <td><%=h @result.name %></td>
 <td><%=h @result.age %></td>
 </tr>

</table>

I am getting this error: "You have a nil object when you didn't
expect it!"
The log shows "NULL" for the selected age whose value should be passed
in as the condition for the query.

So, I wanted to change the primary key just to see if it might make any
difference. I don't know what is wrong with the query.
In SQL, the query is like this:
SELECT name, age  FROM author WHERE age = "the user selected age"

Cypray.

















Bobnation wrote:
> First thing I would ask is "why?" What is the reason behind the
> change?
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2008-12-06 08:16
(Received via mailing list)
On Fri, Dec 5, 2008 at 10:50 PM, Jay Mark
<rails-mailing-list@andreas-s.net>wrote:

>     end
>  </tr>
>
>
>  <% @results.each do |result| %>
>
>  <tr>
>
>  <td><%=h @result.name %></td>
>  <td><%=h @result.age %></td>
>  </tr>
>

Hi, in the above HTML, you should be using result instead of @result.
For
example,

 <td><%=h result.name %></td>
 <td><%=h result.age %></td>

Good luck,

-Conrad
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2008-12-06 16:30
(Received via mailing list)
On Fri, Dec 5, 2008 at 10:50 PM, Jay Mark
<rails-mailing-list@andreas-s.net> wrote:

> def author_age
>    @results = Author.find :all, :conditions => ["age = ?",
> params[:authors]]
>     end

> I am getting this error: "You have a nil object when you didn't
> expect it!"
> The log shows "NULL" for the selected age whose value should be passed
> in as the condition for the query.
>
> So, I wanted to change the primary key just to see if it might make any
> difference. I don't know what is wrong with the query.

Change the primary key? Why not repaint your bedroom? Maybe
put new tires on the car? :-)

I already told you in another thread: params[:authors] IS NOT the
value you think it is. You are NOT PASSING a valid value for "age"
so of course the result set is empty, giving you a nil @results.

Your form doesn't match up with your code. That's the problem.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
This topic is locked and can not be replied to.