Rails how to add belongs_to to gem file - ruby-on-rails-3

This is difficult for me to explain, but I need to add the belongs_to attribute to a model of a gem.
I am using the APN_on_rails gem to add push notification functionality to my web app. In this gem it creates a model called Device (APN::Device) which has a few fields needed for it to work.
I now need to link the Device model to my own User model. I have successfully migrated a user_id to the database table.
Now the only problem is adding belongs_to to the model. It is not in die /app/models/ directory.
How can I achieve this?

It's Ruby, so you can just reopen the class and add more to it.
Create a device.rb file in your models directory and just add the appropriate belongs_to as if you created the whole model yourself. As long as you don't redefine any important methods without calling super it will supplement their model with just what you need. You may also need to add an attr_accessible or attr_protected to be able to use it depending on how they defined their model.
I hope that helps.

Related

Adding 1:M association between 2 model

I Created a Model Mobile and Used Device Gem for login . Now i want to associate these two model such that When i Login Using a User I must be able to see only Mobile related to that User
thanks!
Seems you are new with rails. If this is the case, welcome to the world of magic.
As the answer of your question, create a migration file which should add user_id column in mobiles model/table.
Then add
has_many :mobiles to user.rb model file and
belongs_to :user to mobile.rb model file.
In your controller you can fetch all mobiles related to the currently logged in user by
current_user.mobiles
FYI: Here is Official Tutorial of RubyonRails. It will help you a lot.

Accessing Custom Parameters when using Devise and Rails 4

I am learning rails using the teamtreehouse tutorial. The tutorial uses 3.1 but I am trying to learn 4.0, and as a result I have run into a difficulty presumably because rails 4 forces of the use of strong parameters. I have two models, a users model and a statuses model. I have used devise to create authentication for users, and have included new parameters. They are :first_name, :last_name, and :profile_name. I have created a relationship between users and statuses.
Currently the user sign-up with the new parameters is working; i can access them using for instance current_user.last_name. However, I want to show the profile_name of the user that created the post on the statuses index view(each user does not yet have a separate page). I want to do this using
status.user.profile_name
However it just shows up blank. If I do
status.user.email(which is a preconfigured devise parameter), it shows up no problem. I am guessing I have to whitelist these parameters in some controller but I don't know where or how.
Thanks
I think, here you will find your answer: https://github.com/plataformatec/devise/tree/rails4#strong-parameters
Based on above link, I think you should insert something like this in your ApplicationController:
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:user) { |u| u.permit(:profile_name) }
end
end
And I already suggested in a previous question...
Strong parameters with Rails and Devise
...that you can create your own controller which could extend devise own controller. There is a gist for that:
https://gist.github.com/bluemont/e304e65e7e15d77d3cb9
A little bit more details in Devise doc: https://github.com/plataformatec/devise/tree/rails4#configuring-controllers

Rails 3 + Devise with different model name

I'm sharing one database for two web applications. The User model is already being used for one of these apps, so, in order to sign in to the other one, I had to create another model to avoid mixing users info.
I could make Devise work for this new model, called SystemUser. The problem is now I'll have to use every variable with another name. For example: current_system_user, system_user_signed_in?, etc. I'm using these variables, with their original name, across the whole application, and I would like to know if there's a way to avoid overwriting it. For example: by creating a method called current_user that returns current_system_user, and that way with the other variables mentioned before.
I think this should do the trick:
devise_for :users, class_name: 'SystemUser'
have you considered using CanCan for roles?
https://github.com/ryanb/cancan
I could finally solve the issue by generating the Devise views again. I don't know why, but the devise/sessions folder was missing, and it was doing it with another view, and when I started using it, it worked.
Thanks anyway.

RoR - Foreign Keys created via associations and migrate or "by hand" (or scaffold)?

Just starting to learn Ruby on Rails. I'm using RoR 3. I have read this: http://guides.rubyonrails.org/association_basics.html
But I want to make sure I understand completely.
When creating a new model (I'm doing via scaffold for now), should I specify foreign_key fields at that point, or does the association handle that completely? I believe that association is only at app level, not at the db level, correct?
So I think I must do:
rails generate scaffold post body:text title:string user_id:integer
So in summary, when creating a blog application, must I specify the user_id field in the post model, or does the user model's has_many :posts take care of actually adding that to the db (mine is mysql) when I migrate?
And if the answer is that I should do them when I create the model in the first place (via scaffold or by hand), what happens when I decide later on that I want to add a foreign key, must I add that as an execute statement in a new migration?
You're correct. You need to specify the foreign key when you create your scaffold/model/migration as you stated to get the DB to be correct, and the has_many takes cares of the model for you.
So for initial generation of a scaffold (or model), just do:
rails generate scaffold post body:text title:string user_id:integer
as you stated, and add the has_many for the model itself.
For additions later on, you would make up a new migration, something like (assuming you want to use generation, but you could write your own migration):
rails generate migration add_user_id_to_posts user_id:integer
With that, you can run a rake db:migrate, and then update your model with a has_many or whatever association you need.

vote_fu_rails_3 issue

I'm having trouble with a plugin on a rails 3 app called vote_fu_rails_3. This is a rails 3 compatible version of vote_fu by peteonrails. Both plugins are inspired by acts_as_voteable.
I have a site you can look at http://rubeddit.heroku.com - it's a reddit clone for a class i'm doing.
I am trying to get voting working. I have setup everything according to the instructions on the github page. I have a Link model that has_many :votes and my vote model has belongs_to :user and belongs_to :link.
When trying to do the method to vote_for a link by trying <%= link_to 'up', #user.vote_for(#link) %> I get a SQLite constraint error that voteable_id cannot be NULL.
You can see my code here am i doing something incorrectly? I'm open to any suggestions, i'm pretty new with rails.
Edit: The main issue i'm having seems to stem from voteable_id and voteable_type. These are from polymorphic associations. Since I only have the need to vote on Links (a single model) I guess I have the option of removing the polymorphic associations. Leaving them in place however would also make allowing Comments (yet to be added) to be voted on, fairly simple.