composite_primary_keys - How to pass both from view to controller? - ruby-on-rails

I have installed composite_primary_keys, and set my two primary keys. My delete method works, as it doesn't need to pass any id's. However I am unsure how to get my edit path to function.
If I leave it including only the default :id, I get this message (as expected)
Incorrect number of primary keys for Deal: ["id", "merchant_id"]
I have found that for the edit form to render, I effectively have to make the URL look this this
http://localhost:3000/deals/2,1/edit
My attempts to get both values into the edit_deals_path() have not worked.
Is there a tutorial somewhere on how to use composite_primary_keys gem? The author's website doesn't include this issue.

Related

Order Tags Alphabetically By Default Using Acts As Taggable On

I'm using acts-as-taggable-on to allow my NewsItem and MediaItem models to be tagged. By default, accessing the model's tags using either tag_list for an array of the tag names or directly via an association using tags for collection of ActsAsTaggableOn::Tags, results in the tags in an arbitrary order. I would like them to always be alphabetised. There is no mention in the documentation of setting up a default order, other than a way of maintaining the order the tags were created in (by using acts_as_ordered_taggable in the model).
Obviously I can order them every time I call tags using news_item.tags.order(:name), but I would like this to be the default behaviour throughout the application and don't want to duplicate the ordering wherever I need to use tags.
How can I set up my model so that its tags association always returns its tags in alphabetical order?
I'm afraid that this kind of ordering cannot be done in the current version of acts-as-taggable-on gem. It is apparent from the source code that this gem indeed only supports ordering by IDs (preserves the order of creation) or no ordering at all.
There is a pull request open that would allow ordering by name or any other attribute but the activity of the gem development seems to be quite stale right now (the pull request has been open for over a year already). Perhaps you might try to ask about the status of this pull request there.

Preventing Mongoid 4.0.0 model field coercion of id => _id

I'm using Mongoid 4.0.0 with Rails 4. My models map tables in another application, and I have no control over the field names.
One of the models has a field named id, which is getting coerced into Mongo's _id field. For example, when I insert a document with an id value of "something" I get
{_id:"something", id:null}
instead of
{_id:ObjectId("<hexstring>"),id:"something"}
Is there any way to avoid this coercion, make Mongoid not conflate the two fields, and leave my id field alone?
As I said, renaming the id field is not an option.
Thanks!
[edited]
This is definitely not a MongoDB issue. It must be in Moped or (my guess) Mongoid.
I've tried changing the params key from :id to :_rid but this is still happening. I'm going to check out aliases, but from my first pass I don't think they're going to help -- they appear to go the wrong way.
This appears to be hardcoded into Moingoid and a pervasive assumption throughout. It's annoying enough, though, that I might come up with a patch to allow users to override the key field on a per-model basis.
Oh well.

Rails throwing Unpermitted parameters: name error when using Devise

I am trying to follow this as an example: https://github.com/RailsApps/rails-devise/
I made a project of my own where I try it out and everything is working fine exept for the name part. If I try to make a new user or edit it, rails will throw this Unpermitted parameters: name error.
I have added name as a string into the users table and everything should be fine. I read that some people suggested adding a specific user_params mathod to allow :name but the example I linked before does not have it and in there it all works.
Also worth noting maybe that a direct INSERT INTO in pgAdmin will insert a new row with a name without any problems. So it's something with Rails.
"Unpermitted parameters" means you're doing mass assignment without marking the passed in parameter as permitted. Have another look at this section of the example app documentation: https://github.com/RailsApps/rails-devise/#adding-a-name-attribute. Make sure the name attribute is listed in config/initializers/devise_permitted_parameters.rb.

Model Accepts Nesting Attributes without declaring that it accepts them

everyone. So, I'm working on a basic Rails 4 application for practice, and I have a model for FriendCircle and a model for FriendCircleMembership. (the FriendCircleMembership's corresponding table is basically a join table).
In the console, I'm able to create a new FriendCircle object while passing in :friend_circle_memberships_attributes. This successfully inserts a new FriendCircle row into my table as well as inserting the proper rows into the FriendCircleMembership table.
The WEIRD thing is that, even if i comment out that the FriendCircle accepts_nested_attributes for :friend_circle_memberships, it still works. Is this because i am whitelisting it as a permission in the controller?
The other issue is that, even though i can successfully make the nested objects via the rails console, when i try making it through my html form it says my friend_circle_memberships_attributes is an unpermitted parameter. Not sure why this is happening. I check the incoming parameters and they look fine.
any help would be SWEEEEET. thanks.
I determined what the error was: One of my controllers for a nested attributes was validating the presence of the id of the controller it was nested under.
I'm assuming the validation occurs before an id is created, which makes sense, but im not 100%. So i just took out the validator and things worked.

Obfuscating ids in Rails app

I'm trying to obfuscate all the ids that leave the server, i.e., ids appearing in URLs and in the HTML output.
I've written a simple Base62 lib that has the methods encode and decode. Defining—or better—overwriting the id method of an ActiveRecord to return the encoded version of the id and adjusting the controller to load the resource with the decoded params[:id] gives me the desired result. The ids now are base62 encoded in the urls and the response displays the correct resource.
Now I started to notice that subresources defined through has_many relationships aren't loading. e.g. I have a record called User that has_many Posts. Now User.find(1).posts is empty although there are posts with user_id = 1. My explanation is that ActiveRecord must be comparing the user_id of Post with the method id of User—which I've overwritten—instead of comparing with self[:id]. So basically this renders my approach useless.
What I would like to have is something like defining obfuscates_id in the model and that the rest would be taken care of, i.e., doing all the encoding/decoding at the appropriate locations and preventing ids to be returned by the server.
Is there any gem available or does somebody have a hint how to accomplish this? I bet I'm not the first trying this.
What you are describing sounds like a specialized application of a URL slug. Take a look at plugins like acts_as_sluggable or friendly_id. Also look at overriding the to_param method on your User model.
Maybe start here: Best Permalinking for Rails

Resources