Rails don't show API object if it matches database object - sql

My app pulls in group objects using an API from Meetup.com. These objects don't go into my database. Users can then add a group to their profile, at which point it does enter my database.
When the user logs back into my site, I don't want to show them the group objects from Meetup.com that they've already added to their profile.The shared key here is meetup.com objects have a "group.id" that matches my database "group" model's "group_id"
If the groups from the API were database objects, I imagine I could do something like this:
#groups = RMeetup::Client.fetch(:groups, :lat => #user.latitude, :lon => #user.longitude)
#justnewgroups = #groups.where("id NOT IN (?)", current_user.groups.pluck(:group_id)
But I don't know how to do it without using where. Maybe something like this in the view?:
<% #groups.each do |group| %>
<% unless group.id == current_user.groups.pluck(:group_id) %>
<%= render :partial => 'groups/group', :locals => { :group => group } %>
<% end %>
<% end %>
I'm not getting any errors, but none of the user's groups are being excluded.

new_groups = RMeetup::Client.fetch(:groups, :lat => #user.latitude, :lon => #user.longitude)
current_group_ids = current_user.groups.pluck(:group_id)
#groups = new_groups.reject { |group| current_group_ids.include?(group.id) }

Related

condition in partial from other controller

I have a model group -> has_many :questions and a question -> has_many :votes
In my my view show, for the controller groups, I have a partial that can display the list of the questions :
<%= render :partial => 'question', :collection => #group.questions.byvote %>
Then I have a link to vote with a style like this :
<%= link_to(question_votes_path(question), :controller => :vote, :action => :create, :method => :post, :id => 'vote') do %>
<%= image_tag("voteUp.png")%>
<%= question.votes_count%>
<%end%>
What I would like to do is make a condition, like :
<% if canvote?%>
… and change the style of the div.
BUT. Making a condition in the group controller can't be made because I need to make my request about the question and not about the group :
Vote.where(:user_id => current_user.id, :question_id => #question.id).first
How can I make it form the other controller group or tell an helper_method in the question controller ?
I think you should use a model method - you can then call it right in the view and do so unobtrusively. The following method will return true if the user has voted on a given question:
User.rb
def voted?(question)
#returns true if vote exists
Vote.exists?(:user_id => current_user.id, :question_id => question.id)
end
You can use it like this:
_question.html.erb
<% if current_user.voted?(#question) %>
#code to use if the user has voted on this question
<% else %>
#code to use if the user has NOT voted on this question
<% end %>

How to treat additional parameters on Rails model creation?

I have a register form for users to sign up to my rails app and I added a checkbox for users who do not want a special service. Here is the code from the view :
<%= form_for(#user, :url => { :action => "create" }, :html => { :multipart => true }) do |f| %>
<%= f.check_box :wantsaoc, :onchange => "check_field(this)" %>
[etc...]
<% end %>
In my model i have a wantsaoc method thats returns me a boolean depanding on some other attributes.
How can i handle the creation so that i can catch the wantsaoc parameter and behave depending on it ?
Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state. For example,
class User < AR::Base
..
before_save :do_something
def do_something
if wantsoac.eql?('true')
.. # Do something
end
end
end
Go through various callbacks you can use and choose when and what you need to do.

How to access one model within another model in Rails..?

I have one data model 'object' with fields->object_id, object_name.
That is: http://localhost:3000/objects/
I have created another model 'front_pages' (not created any migration in this, instead I have created some pages like 'search.html.erb'(by hand) and the associated controllers).
That is: http://localhost:3000/front_pages/
My question is: How to access/search the items stored in the 'object' database within the 'search.html.erb'.
"These two are in the same rails project folder"
-> How to display the search results into an HTML.erb file?
views/static_pages/show.html.erb
<% #npsobject.each do |npsobjects| %>
Nps:
Nps type:
Nps name:
|
Static_page Controller
class StaticPagesController < ApplicationController
def show
#npsobject=Npsobject.find(:all, :conditions => ['nps_name LIKE ?', "%#{params[ :search]}%"]);
end
views/static_pages/new.html.erb
<%= form_tag( { :action =>"show"}, { :method => "get"}) do %> # The action path is ok??
<%= text_field_tag :search, params[:search], :class => 'inputBox' %>
"button") %>
Please verify the above codes and guide me through, as Im new to RoR..:)
You need to move your
#npsobject = Npsobject.find
into show action
and then each it into your views/static_pages/show.html.erb
<% #npsobject.each do |nps| %>
<%= nps.nps_name %>
<% end %>

Ransack search form for nested model in Rails 3 app

I want to search and return an index of Users based on their Profile data using Ransack in my Rails 3 app. In the docs it mentions nested attributes but since I'm a beginner I'm not sure how to use the helpers to accomplish what I want.
The ransack docs recommend the following:
users_controller.rb:
def index
#q = User.search(params[:q])
#users = #q.result(:distinct => true)
end
def search
index
render :index
end
routes.rb:
resources :users do
collection do
match 'search' => 'users#search', :via => [:get, :post], :as => :search
end
end
app/views/users/index.html.erb:
<%= search_form_for #q, :url => search_users_path,
:html => {:method => :post} do |f| %>
<% end %>
In the form I've tried to weave in the profile by using:
<%= current_user.profile.high_school %>
<%= f.text_field :profile_high_school_cont %>
Doing this doesn't work, however. The results show nothing when I search for a field belonging to at least one User's profile.
Actually, this does work. I had a f.check_box that was messing things up.

Newbie Question - Finding Data Problems

This is probably a very basic question but I can't figure out what to do.
I need to find a username from an id in an unrelated view.
In my app, my Orders have many Milestones. Each Milestone has an associated user although there is no relationship between the Milestone model and User model. In my Milestones form, I used the following to grab the user id's and display as a name:
<%= f.input :milestone_user, :as => :select, :multiple => false, :input_html => { :size => 1 }, :label => "", :collection => User.find(:all, :order => "name ASC") %>
However, in my show Milestone view, I can only see an id.
In order to display the name, I was trying something like this in the console to test:
#user = User.where(:id => 2)
But when I run:
#user.name
I get a no method error.
My questions are as follows:
Should I just make the milestones belong to Users as well?
If not, what's the best way to display the username?
-- UPDATE --
I'm totally lost about calling this in my view. If I use the suggestion below:
#user = User.find(2)
Actually what we need is something like:
#user = User.find(:milestone_user)
Where milestone_user is an integer. But that gives me errors. Very confused...
-- UPDATE 2 --
In my index view, I'm actually using table builder to display the information. For simplicity, I've deleted the extra code. My home#index view contains:
<% for milestone in orders %>
<li><%= link_to milestone.name, milestone_path(milestone.id), { 'data-href' => milestone.notes, :class => 'selector' } %></li>
<li><%= #users.name %> </li>
<% end %>
And my home controller:
#milestones = Milestone.all
#users = User.find(params[:milestone_user])
Replace #user = User.where(:id => 2) with #user = User.find(2)
UPDATE 2
The easiest way - remove #users = User.find(params[:milestone_user]) from your home#index method and add correct your view file:
<% for milestone in orders %>
<li><%= link_to milestone.name, milestone_path(milestone.id), { 'data-href' => milestone.notes, :class => 'selector' } %></li>
<li><%= User.find(milestone.user_id).name %> </li>
<% end %>