In linking a sports event to two teams, at first this seemed to make sense:
events
- id:integer
- integer:home_team_id
- integer:away_team_id
teams
- integer:id
- string:name
However I am troubled by how I would link that up in the active record model:
class Event
belongs_to :home_team, :class_name => 'Team', :foreign_key => "home_team_id"
belongs_to :away_team, :class_name => 'Team', :foreign_key => "away_team_id"
end
Is that the best solution?
In an answer to a similar question I was pointed to single table inheritance, and then later found polymorphic associations. Neither of which seemed to fit this association. Perhaps I am looking at this wrong, but I see no need to subclass a team into home and away teams since the distinction is only in where the game is played. If I did go with single table inheritance I wouldn't want each team to belong_to an event so would this work?
# app/models/event.rb
class Event < ActiveRecord::Base
belongs_to :home_team
belongs_to :away_team
end
# app/models/team.rb
class Team < ActiveRecord::Base
has_many :teams
end
# app/models/home_team.rb
class HomeTeam < Team
end
# app/models/away_team.rb
class AwayTeam < Team
end
I thought also about a has_many through association but that seems two much as I will only ever need two teams, but those two teams don't belong to any one event.
event_teams
- integer:event_id
- integer:team_id
- boolean:is_home
Is there a cleaner more semantic way for making these associations in active record? or is one of these solutions the best choice?
Thanks
I have -- what I think -- is a simple question. Here's my code:
class Fruit < ActiveRecord::Base
end
class Apple < Fruit
end
class Kiwi < Fruit
end
Assume that I have all the STI setup correctly, and there are multiple types of Apple and Kiwi records in the table. From here...
fruits = Fruit.find(:all)
...how do I return an array of just Apples from the fruits array?
I have developed a blog application of sorts that I am trying to allow other users to take advantage of (for free and mostly for family). I wondering if the authentication I have set up will allow for such a thing. Here is the scenario.
Currently the application allows for users to sign up for an account and when they do so they can create blog posts and organize those posts via tags. The application displays no data publicly (another words, you have to login to see anything). To gain access you have to create an account and even after you do, you cannot see anyone else's information as the applications filters using the current_user method and displays in the /posts/index.html.erb page. This would be great if a user only wanted to blog and share it with themselves, not really what I am looking for.
My question has two parts (hopefully I won't make anyone mad by not putting these into two questions)
Is it possible for a particular users data to live at www.myapplication.com/user without moving everything to the /user/show.html.erb file?
Is it possible to make some of that information (living at the URL) public but still require login for create and destroy actions.
Essentially, exactly like twitter. I am just curious if I can get from where I am (using the current_user methods across controllers to display in /posts/index.html.erb) to where I want to be. My fear is that I have to redesign the app such that the user data lives in the /user/show.html.erb page.
Thoughts?
UPDATE:
I am using Clearance for authentication by Thoughtbot. I wonder if there is something I can set in the vendored gem path to represent the /posts/index.html.erb code as the /user/id code and replace id with the user name.
I have developed a blog application of sorts that I am trying to allow other users to take advantage of (for free and mostly for family). I wondering if the authentication I have set up will allow for such a thing. Here is the scenario.
Currently the application allows for users to sign up for an account and when they do so they can create blog posts and organize those posts via tags. The application displays no data publicly (another words, you have to login to see anything). To gain access you have to create an account and even after you do, you cannot see anyone else's information as the applications filters using the current_user method and displays in the /posts/index.html.erb page. This would be great if a user only wanted to blog and share it with themselves, not really what I am looking for.
My question has two parts (hopefully I won't make anyone mad by not putting these into two questions)
Is it possible for a particular users data to live at www.myapplication.com/user without moving everything to the /user/show.html.erb file?
Is it possible to make some of that information (living at the URL) public but still require login for create and destroy actions.
Essentially, exactly like twitter. I am just curious if I can get from where I am (using the current_user methods across controllers to display in /posts/index.html.erb) to where I want to be. My fear is that I have to redesign the app such that the user data lives in the /user/show.html.erb page.
Thoughts?
Googled for this to no avail. Didn't find anything in the API either. I was expecting some kind of class method or configuration option to set it...
So, rather than calling
from "[email protected]"
for every method, it could be called automatically.
Does anybody know of a plug-and-play login system that supports existing logins like Google and OpenID?
I am looking to implement something similar to that of Stack Overflows.
Thanks!
I have a model for which I want to retrieve the next record(s) and previous record(s). I want to do this via a named_scope on the model, and also pass in as an argument the X number of next/previous records to return.
For example, let's say I have 5 records:
Record1
Record2
Record3
Record4
Record5
I want to be able to call Model.previous or Model.previous(1) to return Record2. Similarly, I want to be able to call Model.next or Model.next(1) to return Record4. As another example I want to be able to call Model.previous(2) to return Record3. I think you get the idea.
How can I accomplish this?
Hi There,
Continuing with my adventure to convert COBOL to a Ruby program, I have to convert a decimal digit to a comp-3/packed decimal format. Anyone know of a simple Ruby script or gem that does this?
Berns
class Question < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :questions
end
When I find some questions, I usually need to get their authors at the same time, so I use:
Question.find(:all, :include=>:authors)
But I don't write the ":include" part everywhere. I hope I can define the "include" somewhere only once, and when I find questions, the author will be automaticly loaded. Is there any way to do this?
I've written this class that returns feed updates, but am thinking it can be further improved. It's not glitchy or anything, but as a new ruby developer, I reckon it's always good to improve :-)
class FeedManager
attr_accessor :feed_object, :update, :new_entries
require 'feedtosis'
def initialize(feed_url)
@feed_object = Feedtosis::Client.new(feed_url)
fetch
end
def fetch
@feed_object.fetch
end
def update
@updates = fetch
end
def updated?
@updates.new_entries.count > 0 ? true : false
end
def new_entries
@updates.new_entries
end
end
As you can see, it's quite simple, but the things I'm seeing that aren't quite right are:
Whenever I call fetch via terminal, it prints a list with the updates, when it's really supposed return an object.
So as an example, in the terminal if I do something like:
client = Feedtosis::Client.new('http://stackoverflow.com/feeds')
result = client.fetch
I then get:
<Curl::Easy http://stackoverflow.com/feeds>
Which is exactly what I'd expect. However, when doing the same thing with "inniting" class with:
FeedManager.new("http://stackoverflow.com/feeds")
I'm getting the object returning as an array with all the items on the feed.
Sure I'm doing something wrong, so any help refactoring this class will he greatly appreciated.
Also, I'd like to see comments about my implementation, as well as any sort of comment to make it better would be welcome.
Thanks in advance
Hi,
is it possible to set/read cross-domain cookies in Opera browser? I'm using solution http://bit.ly/c1Tk1i (sorry - in Russian, plz use google translate) and it works fine in any browser except Opera.
read/write_attribute is a great way to enhance default accessors generated by ActiveRecord. Like this for example:
def price
read_attribute(:price) or "This item is priceless and you are by the way #{User.current.login}"
end
The same however does not seem to be working with associations.
Demonstration:
class Product < ActiveRecord::Base
has_and_belongs_to_many :stores
end
Then
>> a = Product.first
=> #<Product id: 1, name: "awesome product", created_at: "2010-05-07 12:11:00", updated_at: "2010-05-07 12:11:00">
>> a.stores
=> [#<Store id: 1, name: "ikea", created_at: "2010-05-07 12:11:28", updated_at: "2010-05-07 12:11:28">]
>> a.read_attribute(:stores)
=> nil
>>
So, is there some sort of read/write_association? Or, if not, is there a reason not to have one?
I'm wondering if theres a best practice for what I'm trying to accomplish...
First we have the model categories, categories, has_many posts.
Now lets say, users add posts.
Now, I have a page, that I want to display only the current user's posts by category.
Lets say we have the following categories: A, B, and C
User 1, has posted in Categories A and B.
In my view I have something like:
<% @categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% category.posts.select {|post| post.user == current_user}.each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
For the most part its almost there. The issue is, it will still show an empty category if any posts have been entered in it at all (even if the current user has not input posts into that category.
So the question boils down to:
How do I make the following loop only count posts in the category from the current user?
<% @categories.select {|cat| cat.posts.count 0}.each do |category| %
Best,
Elliot
I've got a web app where I'd like to user FTS3 functionality of SQLite3.
I've got the SQLite3-ruby v1.2.5 gem installed. I'd like to have FTS3 support, and I know, due to trying to create an FTS3 table, that it doesn't come with it.
Do I need to compile the gem/sqlite3 myself in order to get this support? Is there a seprate gem I should be using?
Songs on Rap Genius have paths like /lyrics/The-notorious-b-i-g-ft-mase-and-puff-daddy/Mo-money-mo-problems which are defined in routes.rb as:
map.song '/lyrics/:artist_slug/:title_slug', :controller => 'songs', :action => 'show'
When I want to generate such a path, I use song_url(:title_slug => song.title_slug, :artist_slug => song.artist_slug). However, I'd much prefer to be able to type song_url(some_song). Is there a way I can make this happen besides defining a helper like:
def x_song_path(song)
song_path(:title_slug => song.title_slug, :artist_slug => song.artist_slug)
end
I've been having almost the same issues as Victor Martin (you can see the questions asked here).
I've got declarative authorization working for just about everything that doesn't involve using conditionals. E.g.
has_permission_on :users, :to => [:edit, :update, :destroy] do
if_attribute :user => is { current_user }
end
Are there any common pitfalls with Declarative Authorization? I'm using authlogic and I'm suspicious the 'current_user' method in the application controller might be the source of the problem.
Whats the best way to enable users to log in with their email address OR their username? I am using warden + devise for authentication. I think it probably won't be too hard to do it but i guess i need some advice here on where to put all the stuff that is needed. Perhaps devise devise already provides this feature? like in the config/initializers/devise.rb you would write:
config.authentication_keys = [ :email, :username ]
To require both username AND email for signing in. But i really want to have only one field for both username and email and require only one of them. I'll just visualize that with some ASCII art, it should look something like this in the view:
Username or Email:
[____________________]
Password:
[____________________]
[Sign In]
Hey Guys,
I'm trying to build a multi model form - but one of the issues, is I need to link the models within the form.
For example, lets say the form I have has the following models: Users, Profiles
When creating a new user, I'd like to create a new profile simultaneously, and then link the two. The issue is, if neither have been created yet, they don't have ID's yet - so how can I assign linking values?
Thanks!
-Elliot
I have a number of models that use STI and I would like to use the same unit test to test each model. For example, I have:
class RegularList < List
class OtherList < List
class ListTest < ActiveSupport::TestCase
fixtures :lists
def test_word_count
list = lists(:regular_list)
assert_equal(0, list.count)
end
end
How would I go about using the test_word_count test for the OtherList model. The test is much longer so I would rather not have to retype it for each model. Thanks.
I am using Factory Girl but like the machinist syntax. So I wonder, if there is any way creating a named blueprint for class, so that I can have something like that:
User.blueprint(:no_discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader false
name "foolish"
salt "21746899800.223524289203464"
end
User.blueprint(:discount_user) do
admin false
hashed_password "226bc1eca359a09f5f1b96e26efeb4bb1aeae383"
is_trader true
name "deadbeef"
salt "21746899800.223524289203464"
discount_rate { DiscountRate.make(:rate => 20.00) }
end
DiscountRate.blueprint do
rate {10}
not_before ...
not_after ...
end
Is there a way making factory_girl with machinist syntax acting like that? I did not find one. Help appreciated.
Thx in advance
Jason
I am trying to test facebook api calls with cucumber. Here is the code.
# app/controller/facebook_users_controller.rb
class FacebookUsersController < ApplicationController
def create
fb_user = facebook_session.user
user = User.new(:facebook_uid => fb_user.uid, :facebook_session_key => facebook_session.session_key
respond_to do |format|
if user.save
format.json { render :json => { :status => 'ok' }.to_json }
end
end
end
end
# features/steps/facebook_connect_step.rb
Given /^I am a facebook connected user$/ do
mock_session = Facebooker::MockSession.create
post('/facebook_user.json')
puts response.code
end
When I run the cucumber step for above step definition, I get a response code of 406 instead of 200. This happens in the cucumber test environment only and not in the browser(development/production).
Hi all,
I tried running bundle install in our production server, but I encounter this problem:
Updating
git://github.com/collectiveidea/delayed_job.git
fatal: Refusing to fetch into current
branch refs/heads/master of non-bare
repository An error has occurred in
git. Cannot complete bundling.
I have bundler version 0.9.25 installed.
Thanks
I'm trying to define a step to test the value of alt text of an image using capybara and the css selectors.
I wrote one for input values based on the readme examples:
Then /^I should see a value of "([^\"])" within the "([^\"])" input$/ do |input_value, input_id|
element_value = locate("input##{input_id}").value
element_value.should == input_value
end
But I can't figure this one out...something like:
Then /^I should see the alttext "([^\"]*)"$/ do | alt_text |
element_value = locate("img[alt]").value
Anyone know how I can locate the alt text value?