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.
Ff3c3206118733da78f58d749a5e4311?d=identicon&s=25 Scott Peterson (golddog)
on 2008-12-08 22:37
Attachment: player.rb (480 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.
B1b1d33e0655e841d4fd8467359c58d0?d=identicon&s=25 Yossef Mendelssohn (Guest)
on 2008-12-09 00:38
(Received via mailing list)
On Dec 8, 3:31 pm, Scott Peterson <s...@mho.com> 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.
Ff3c3206118733da78f58d749a5e4311?d=identicon&s=25 Scott Peterson (golddog)
on 2008-12-09 00:51
Yossef Mendelssohn wrote:
> On Dec 8, 3:31?pm, Scott Peterson <s...@mho.com> 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.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-12-09 01:11
(Received via mailing list)
Scott Peterson 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!
Ff3c3206118733da78f58d749a5e4311?d=identicon&s=25 Scott Peterson (golddog)
on 2008-12-09 01:15
Tim Hunter wrote:
> Scott Peterson 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.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-12-09 01:28
(Received via mailing list)
Scott Peterson 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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2008-12-09 02: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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-09 16:38
(Received via mailing list)
Scott Peterson 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?
B1b1d33e0655e841d4fd8467359c58d0?d=identicon&s=25 Yossef Mendelssohn (Guest)
on 2008-12-09 17:06
(Received via mailing list)
On Dec 8, 6:09 pm, Scott Peterson <s...@mho.com> 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.