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