Forum: Ruby Beginner question about attributes

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.
Scott P. (Guest)
on 2008-12-08 23:37
Attachment: player.rb (0 Bytes)
I don't understand why my model (attached) isn't working.

I generated a scaffold for my 'player' data.  If I uncomment the
:validates_presence_of check in my model, the save on the 'new' page
compalins about those fields, and re-displays them without the data I've
typed in (along with the red surrounding and error messages, of course).

If I run the model as is, the save goes through, but none of the fields
except hashed_password get saved.  Or, another way to put it is, the
*only* field that gets saved is the one I set manually in the
before_create handler.

I don't understand what I did.  I haven't changed the generated view for
new.html.erb, the column names haven't changed in the database.  I don't
see what's missing to reconnect those elements.
Yossef M. (Guest)
on 2008-12-09 01:38
(Received via mailing list)
On Dec 8, 3:31 pm, Scott P. <removed_email_address@domain.invalid> wrote:
> If I run the model as is, the save goes through, but none of the fields
> except hashed_password get saved.  Or, another way to put it is, the
> *only* field that gets saved is the one I set manually in the
> before_create handler.

1. This is a Rails question, not a Ruby question.

2. This has probably much more to do with your controller than your
model.
Scott P. (Guest)
on 2008-12-09 01:51
Yossef M. wrote:
> On Dec 8, 3:31?pm, Scott P. <removed_email_address@domain.invalid> wrote:
>> If I run the model as is, the save goes through, but none of the fields
>> except hashed_password get saved. ?Or, another way to put it is, the
>> *only* field that gets saved is the one I set manually in the
>> before_create handler.
>

Thanks for the response.  I guess there's a lot I don't understand.

> 1. This is a Rails question, not a Ruby question.

I thought that Ruby was the language in which all the controllers,
models, and views were written.  If I've generated a scaffold and only
changed the model file, it seems like that's where I've introduced the
bug(?)

>
> 2. This has probably much more to do with your controller than your
> model.

Something in the generated create, where it initializes as such:

@player = Player.new(params[:player]).  I'll keep poking around to see
if I can figure out why those params aren't what I think they should be.
Tim H. (Guest)
on 2008-12-09 02:11
(Received via mailing list)
Scott P. wrote:
>
> I thought that Ruby was the language in which all the controllers,
> models, and views were written.  If I've generated a scaffold and only
> changed the model file, it seems like that's where I've introduced the
> bug(?)
>

You're correct. However, Ruby is a general-purpose programming language
used for many things besides Rails. This list is generally frequented by
Ruby users of all stripes and, while there may be a few Rails users
around, you will have more luck getting an answer to your question in
the Rails forum.

See http://www.rubyonrails.com/community

Good luck!
Scott P. (Guest)
on 2008-12-09 02:15
Tim H. wrote:
> Scott P. wrote:
>>
>> I thought that Ruby was the language in which all the controllers,
>> models, and views were written.  If I've generated a scaffold and only
>> changed the model file, it seems like that's where I've introduced the
>> bug(?)
>>
>
> You're correct. However, Ruby is a general-purpose programming language
> used for many things besides Rails. This list is generally frequented by
> Ruby users of all stripes and, while there may be a few Rails users
> around, you will have more luck getting an answer to your question in
> the Rails forum.
>
> See http://www.rubyonrails.com/community
>
> Good luck!

Ah, thanks for the clarification.  Yossef got me thinking down a
different path, which led me to making those attributes accessible,
seems to be working (well enough for me--as I learn more, I'll probably
figure out that needs to be changed).

Anyway, I appreciate the feedback.
Tim H. (Guest)
on 2008-12-09 02:28
(Received via mailing list)
Scott P. wrote:
>
> Ah, thanks for the clarification.  Yossef got me thinking down a
> different path, which led me to making those attributes accessible,
> seems to be working (well enough for me--as I learn more, I'll probably
> figure out that needs to be changed).
>
> Anyway, I appreciate the feedback.

Don't feel alone. We get one or two new Rails users a week, sometimes
more. After you master Rails maybe you'll want to use Ruby for something
else. If so, come on back. We'll still be here.
Robert H. (Guest)
on 2008-12-09 03:21
> After you master Rails maybe you'll want to use Ruby for something
> else.

Like RMagick :)

Or for complete world dominance.

I may have some script for that .... world_control.rb

It is unfinished though.
Phlip (Guest)
on 2008-12-09 17:38
(Received via mailing list)
Scott P. wrote:

> I thought that Ruby was the language in which all the controllers,
> models, and views were written.  If I've generated a scaffold and only
> changed the model file, it seems like that's where I've introduced the
> bug(?)

A technical question always gets the best answer on the narrowest
possible
forum. To help the Rails forum succeed, we who know Rails try not to
answer
questions about it here; that dilutes both groups.

> @player = Player.new(params[:player]).  I'll keep poking around to see
> if I can figure out why those params aren't what I think they should be.

How are your unit tests?
Yossef M. (Guest)
on 2008-12-09 18:06
(Received via mailing list)
On Dec 8, 6:09 pm, Scott P. <removed_email_address@domain.invalid> wrote:
> Ah, thanks for the clarification.  Yossef got me thinking down a
> different path, which led me to making those attributes accessible,
> seems to be working (well enough for me--as I learn more, I'll probably
> figure out that needs to be changed).

To be honest, I didn't really look closely at the model because I had
already categorized this as a Rails question by that point.

There was no way to be absolutely positive given the information
provided, but it could be reasonable to assume you were using a
generated controller with standard actions. Really, the problem was in
the model with the use of attr_accessible, which keeps other
attributes from being mass-assigned.

Of course, the question was still in the wrong place, much like
attr_accessible and attr_protected are in the wrong part of the MVC
trio. But that's a discussion for another day.
This topic is locked and can not be replied to.