Adding School Condition

For my implementation I would like any user who is logged in to be able
to add a school (object.) However, I would like this school to only
become visible to that and all other users, including non-users if four
other people have tried to add that school with the exact same school
name. In this way, I hope that it will keep school names legitimate and
clean.

I can think of some implementations of how to do this. But I’m wondering
what’s the best, and simplest way to do this?

perhaps something as simple as:

@school = School.find_by_name(params[:school][:name])
@school ||= School.create(:name => params[:school][:name], :other_field
=>
params[:school][:other])
@school.increment!(:verified_by)

On Dec 10, 2007 11:13 AM, Ellis B.
[email protected]
wrote:


Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

I’m sorry could you explain your code some more?

perhaps something as simple as:

@school = School.find_by_name(params[:school][:name])
@school ||= School.create(:name => params[:school][:name], :other_field
=>
params[:school][:other])
@school.increment!(:verified_by)

The code will either find a school if there is a school in the database
which matches params[:school][:name] or return nil if there is not a
school
that has that name.

If it happens to be nil, then it will create a school object with that
name.
:other_field is just a placeholder for any other fields you want to put
in
to the school record.

Then because by now it should have a school object, it increments the
column
called “verified_by” by one.

On Dec 10, 2007 1:07 PM, Ellis B. [email protected]
wrote:

@school.increment!(:verified_by)

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

OK. This works great. But can I add a twist?
The school includes a column for user_id, so when searching for a school
the code

@school = School.find_by_title([:school][:title])

will not work as it will return an array. In addition, any user that has
contributed to making the verified increment cannot add the school
again, thus cheating the system of only unique users. Please advise,
thank you.

I don’t see how School.find_by_title would return an array, unless
you’ve
customised the method.

As for saving who has verified and who hasn’t maybe it might be easy to
serialize an array into the schools table.

Add serialize :verified_by into your model and then you should be able
to
write to this field as if it were an array. This has changed from my
previous suggestion of incrementing a field with the same name.

@school = School.find_by_title(params[:school][:title])
@school ||= School.create(params[:school].merge({:verified_by => “—
[]”})
@school.verified_by << current_user unless @school.verified_by.include?
(current_user)
@school.save!

On Dec 10, 2007 3:30 PM, Ellis B. [email protected]
wrote:

thank you.

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

It says undefined method `include?’ for 0:Fixnum Hmmm…

The — [] is setting the default value (an empty array) of the
verified_by
field. If that wasn’t done then you would get an error because you would
be
effectively calling << on nil.

On Dec 10, 2007 4:00 PM, Ryan B. [email protected] wrote:

@school = School.find_by_title(params[:school][:title])

OK. This works great. But can I add a twist?

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net


Ryan B.
http://www.frozenplague.net

This implementation has lost its ability to increment up depending on if
the user has not already incremented that particular school. In addition
I keep getting that error. How do I know the field has turned into an
array?

Assuming you’ve used migrations.

The column should be a string and the default should be “— []”. No
reason
for it to give you a 0 number unless you set the field as an integer.

On Dec 10, 2007 4:21 PM, Ellis B. [email protected]
wrote:

It says undefined method `include?’ for 0:Fixnum Hmmm…

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

Remove the field and re add it. Having it as an integer will not work!

Re-add it as a string with the default of “— []”

If you read through one of my previous emails I mentioned that
verified_by
is no longer being incremented.

*VERIFIED BY SHOULD NO LONGER BE INCREMENTED!
*
Verified by is now kept as a serialized array in the database.

On Dec 10, 2007 4:33 PM, Ellis B. [email protected]
wrote:

This implementation has lost its ability to increment up depending on if
the user has not already incremented that particular school. In addition
I keep getting that error. How do I know the field has turned into an
array?

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

class School < ActiveRecord::Base
has_many :courses
has_many :works
acts_as_rated :no_rater => true
serialize :user_id
end

def create
@school = School.find_by_title(params[:school][:title])
@school ||= School.create(params[:school].merge({ :user_id => “—
[]” }))
@school.user_id << current_user unless
@school.user_id.include?(current_user)
@school.save!

redirect_to schools_path

end

  t.string :user_id, :default => "--- []"

all equals a no go for storing the users user_id in the array column

no error. only no functionality. the column just says — when i query
with mysql

Is it still giving the Fixnum error?

On Dec 10, 2007 4:39 PM, Ellis B. [email protected]
wrote:

@school ||= School.create(params[:school].merge({ :user_id => "—

all equals a no go for storing the users user_id in the array column

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

Aha! Don’t query it with MySQL.

Go into script/console and try

@school = School.find(:first)
@school.user_id

And tell me what you get back

I also recommend changing user_id to user_ids to signify that it holds
more
than one, but it’s just a cosmetic change.

On Dec 10, 2007 4:45 PM, Ellis B. [email protected]
wrote:

no error. only no functionality. the column just says — when i query
with mysql

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

That works great. You are so much help thank you. Any clues why this
code is not working?

for id in @school.user_ids
if current_user != id
@school.increment!(:verified_count)
break
end
end

current_user is a user object, id is a number. Try current_user.id

On Dec 10, 2007 5:20 PM, Ellis B. [email protected]
wrote:


Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

Ellis B. wrote:

Still increments with .id appended.

@school.user_ids
=> [#<YAML::Object:0x174ef18
@ivars={“attributes”=>{“salt”=>“fe4249ff4cd74653efd009291740566cbde432ee”,
“updated_at”=>“2007-12-10 01:33:22”,
“crypted_password”=>“ae0a7781810a57d56a2cb4e33c18f5b6d64da703”,
“remember_token_expires_at”=>nil, “id”=>“1”, “remember_token”=>nil}},
@class=“User”>]

Still increments with .id appended.

My fault!

@school.verified_by << current_user

Should be current_user.id

On Dec 10, 2007 5:28 PM, Ellis B. [email protected]
wrote:

@class=“User”>]

Posted via http://www.ruby-forum.com/.


Ryan B.
http://www.frozenplague.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs