Adding 1:M association between 2 model - ruby-on-rails-3

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.

Related

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

Devise, Omniauth and multiple models with STI

I have an application setup with devise authentication using sti (base user model and two other models - company and individual - inheriting from it). From a devise perspective, everything is working. I can have different routes for signup forms and everything works as expected. Now I wanted to give both users (company and individual) the option to signup/sign in using facebook or linked in. If I set the :omniauthable on both models, and set the devise_for on my routes.rb for each model, I get an error saying that only one model can be omniauthable. If I don't use the devise_for for each model, no routes are generated. If I set the omniauthable on user model only, I get only one route and one callback.
I've read somewhere that the solution would be to use omniauth on its own (separated from devise). However, I can't seem to achieve the intended behavior using omniauth separate from devise (I can get one single authorize/callback route, but the two, as intended).
Anyone out there who can help ?
TIA
Devise's Omniauthable module does support multiple models. See
https://github.com/plataformatec/devise/wiki/OmniAuth-with-multiple-models

Rails how to add belongs_to to gem file

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.

Odd cancan issue... works on development machine, but not the server

My ability model for cancan has been working well for 6+ months now. One of the controllers that has been around all that time is the QuestionsController. I recently added another controller, model and index page. The controller name is QuestionsBasicsController. In my ability model i have:
if user.permission >= 8 # admin
can :manage, [Question, QuestionsBasic]
end
At the top of QuestionsController and QuestionsBasicsController i have:
load_and_authorize_resource
I am able to access the index page for both QuestionsController and QuestionsBasicsController on my development machine. After I uploaded the ability model to my server i restarted the server. I can access the index page for Question but not for QuestionsBasic on my server. On my development machine i have ruby 1.9.2 while on my server i have 1.8.7. Could it be the Ruby version difference?
UPDATE: for what it is worth names of models that begin with the name of another model cause a problem,... at least with the way I have constructed my ability model. In addition, I recently found another model name that worked fine but for CanCan. I think it may have been a reserved word but am not positive on that. When I changed the name of the model, controller, route and code to a new name it passed CanCan. No reflection on CanCan though... I think it is AWESOME, AWESOME, AWESOME! Thanks, Ryan Bates!
agree with dleatham, try splitting your original statement into 2 lines:
if user.permission >= 8 # admin
can :manage, Question
can :manage, QuestionsBasic
end

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.